Skip to content

Commit fa2a51d

Browse files
pergerchDresel
andcommitted
Fix EnumLookup for owned entities #16
Co-Authored-By: Christopher Dresel <[email protected]>
1 parent dc2f146 commit fa2a51d

File tree

7 files changed

+224
-117
lines changed

7 files changed

+224
-117
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@ public partial class MyContext : DbContext
2525
2626
protected override void OnModelCreating(ModelBuilder modelBuilder)
2727
{
28+
// Any custom model configuration should come before calling ConfigureEnumLookup and ConfigureNames
29+
// ...
30+
2831
modelBuilder.ConfigureEnumLookup(EnumLookupOptions.Default.UseStringAsIdentifier());
2932

3033
modelBuilder.ConfigureNames(NamingOptions.Default.SkipTableNamingForGenericEntityTypes());
3134

35+
// Any configuration after calling the two methods will not be processed by this Extension
3236
// ...
3337
}
3438
}
3539
```
3640

3741
## Configuration Options
3842

39-
The two extension methods in the `OnModelCreating` method in the `DbContext` class:
43+
There are two extension methods in the `OnModelCreating` method in the `DbContext` class:
4044

4145
- **ConfigureEnumLookup(...)** allows you to define in which form lookup tables for *Enums* will be constructed and named:
4246

@@ -46,6 +50,7 @@ The two extension methods in the `OnModelCreating` method in the `DbContext` cla
4650
- **SetNamingScheme(...)** allows you to override the naming using one of the predefined schemes (see below) or a custom function.
4751
- **UseEnumsWithAttributeOnly()** to generate the enum lookup tables only for enums marked with the `[EnumLookup]` attribute
4852
- **SetDeleteBehavior(...)** to configure the delete behavior for the generated FKs, using the `Microsoft.EntityFrameworkCore.DeleteBehavior` enum (defaults to _Cascade_)
53+
- **Please note:** For owned entities, you should call the **ConfigureOwnedEnumLookup(...)** method on the `OwnedNavigationBuilder`. Please see [#16](/../../issues/16) for more details. E.g. `modelBuilder.Entity<Customer>().OwnsOne(x => x.Address, ownedBuilder => ownedBuilder.ConfigureOwnedEnumLookup(...));`
4954

5055
- **ConfigureNames(...)** allows you to define in which form tables, properties and constraints will be named:
5156

samples/SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo/Data/DemoContext.cs

+34-23
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
3838

3939
protected override void OnModelCreating(ModelBuilder modelBuilder)
4040
{
41+
// Any custom model configuration should come before calling ConfigureEnumLookup and ConfigureNames
42+
modelBuilder.Entity<Product>()
43+
.OwnsMany<Review>(nameof(Product.Reviews),
44+
builder =>
45+
{
46+
builder.ConfigureOwnedEnumLookup(EnumLookupOptions.Default.Pluralize().UseStringAsIdentifier(), modelBuilder);
47+
});
48+
4149
modelBuilder.ConfigureEnumLookup(EnumLookupOptions.Default.Pluralize().UseStringAsIdentifier());
4250

4351
modelBuilder.ConfigureNames(NamingOptions.Default.Pluralize()
@@ -47,29 +55,32 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
4755
.SkipTableNamingForGenericEntityTypes());
4856

4957
modelBuilder.Entity<Product>()
50-
.HasData(new Product
51-
{
52-
ProductId = 1,
53-
ProductCategory = ProductCategory.Book,
54-
Name = "Robinson Crusoe",
55-
ReleaseDate = new DateTime(1719, 4, 25),
56-
Price = 14.99,
57-
}, new Product
58-
{
59-
ProductId = 2,
60-
ProductCategory = ProductCategory.Bluray,
61-
Name = "Rogue One: A Star Wars Story",
62-
ReleaseDate = new DateTime(2017, 5, 4),
63-
Price = 11.99,
64-
}, new Product
65-
{
66-
ProductId = 3,
67-
ProductCategory = ProductCategory.CD,
68-
Name = "Wham! - Last Christmas",
69-
ReleaseDate = new DateTime(1984, 12, 3),
70-
Price = 6.97,
71-
IdealForSpecialOccasion = SpecialOccasion.Christmas,
72-
});
58+
.HasData(
59+
new Product
60+
{
61+
ProductId = 1,
62+
ProductCategory = ProductCategory.Book,
63+
Name = "Robinson Crusoe",
64+
ReleaseDate = new DateTime(1719, 4, 25),
65+
Price = 14.99,
66+
},
67+
new Product
68+
{
69+
ProductId = 2,
70+
ProductCategory = ProductCategory.Bluray,
71+
Name = "Rogue One: A Star Wars Story",
72+
ReleaseDate = new DateTime(2017, 5, 4),
73+
Price = 11.99,
74+
},
75+
new Product
76+
{
77+
ProductId = 3,
78+
ProductCategory = ProductCategory.CD,
79+
Name = "Wham! - Last Christmas",
80+
ReleaseDate = new DateTime(1984, 12, 3),
81+
Price = 6.97,
82+
IdealForSpecialOccasion = SpecialOccasion.Christmas,
83+
});
7384
}
7485
}
7586
}

samples/SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo/Entities/Product.cs

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo.Entities
77
{
88
using System;
9+
using System.Collections.Generic;
910

1011
public class Product
1112
{
@@ -27,5 +28,7 @@ public Product()
2728
public int ProductId { get; set; }
2829

2930
public DateTime ReleaseDate { get; set; }
31+
32+
public List<Review> Reviews { get; set; }
3033
}
3134
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// <copyright file="Review.cs" company="Spatial Focus">
2+
// Copyright (c) Spatial Focus. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
namespace SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo.Entities
7+
{
8+
public class Review
9+
{
10+
public ReviewRating Rating { get; set; }
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// <copyright file="ReviewRating.cs" company="Spatial Focus">
2+
// Copyright (c) Spatial Focus. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
namespace SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo.Entities
7+
{
8+
public enum ReviewRating
9+
{
10+
Excellent = 1,
11+
12+
Average,
13+
14+
Bad,
15+
}
16+
}

0 commit comments

Comments
 (0)