Skip to content

Commit

Permalink
Fix EnumLookup for owned entities #16
Browse files Browse the repository at this point in the history
Co-Authored-By: Christopher Dresel <[email protected]>
  • Loading branch information
pergerch and Dresel committed Sep 16, 2020
1 parent dc2f146 commit fa2a51d
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 117 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ public partial class MyContext : DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Any custom model configuration should come before calling ConfigureEnumLookup and ConfigureNames
// ...
modelBuilder.ConfigureEnumLookup(EnumLookupOptions.Default.UseStringAsIdentifier());

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

// Any configuration after calling the two methods will not be processed by this Extension
// ...
}
}
```

## Configuration Options

The two extension methods in the `OnModelCreating` method in the `DbContext` class:
There are two extension methods in the `OnModelCreating` method in the `DbContext` class:

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

Expand All @@ -46,6 +50,7 @@ The two extension methods in the `OnModelCreating` method in the `DbContext` cla
- **SetNamingScheme(...)** allows you to override the naming using one of the predefined schemes (see below) or a custom function.
- **UseEnumsWithAttributeOnly()** to generate the enum lookup tables only for enums marked with the `[EnumLookup]` attribute
- **SetDeleteBehavior(...)** to configure the delete behavior for the generated FKs, using the `Microsoft.EntityFrameworkCore.DeleteBehavior` enum (defaults to _Cascade_)
- **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(...));`

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Any custom model configuration should come before calling ConfigureEnumLookup and ConfigureNames
modelBuilder.Entity<Product>()
.OwnsMany<Review>(nameof(Product.Reviews),
builder =>
{
builder.ConfigureOwnedEnumLookup(EnumLookupOptions.Default.Pluralize().UseStringAsIdentifier(), modelBuilder);
});

modelBuilder.ConfigureEnumLookup(EnumLookupOptions.Default.Pluralize().UseStringAsIdentifier());

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

modelBuilder.Entity<Product>()
.HasData(new Product
{
ProductId = 1,
ProductCategory = ProductCategory.Book,
Name = "Robinson Crusoe",
ReleaseDate = new DateTime(1719, 4, 25),
Price = 14.99,
}, new Product
{
ProductId = 2,
ProductCategory = ProductCategory.Bluray,
Name = "Rogue One: A Star Wars Story",
ReleaseDate = new DateTime(2017, 5, 4),
Price = 11.99,
}, new Product
{
ProductId = 3,
ProductCategory = ProductCategory.CD,
Name = "Wham! - Last Christmas",
ReleaseDate = new DateTime(1984, 12, 3),
Price = 6.97,
IdealForSpecialOccasion = SpecialOccasion.Christmas,
});
.HasData(
new Product
{
ProductId = 1,
ProductCategory = ProductCategory.Book,
Name = "Robinson Crusoe",
ReleaseDate = new DateTime(1719, 4, 25),
Price = 14.99,
},
new Product
{
ProductId = 2,
ProductCategory = ProductCategory.Bluray,
Name = "Rogue One: A Star Wars Story",
ReleaseDate = new DateTime(2017, 5, 4),
Price = 11.99,
},
new Product
{
ProductId = 3,
ProductCategory = ProductCategory.CD,
Name = "Wham! - Last Christmas",
ReleaseDate = new DateTime(1984, 12, 3),
Price = 6.97,
IdealForSpecialOccasion = SpecialOccasion.Christmas,
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo.Entities
{
using System;
using System.Collections.Generic;

public class Product
{
Expand All @@ -27,5 +28,7 @@ public Product()
public int ProductId { get; set; }

public DateTime ReleaseDate { get; set; }

public List<Review> Reviews { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// <copyright file="Review.cs" company="Spatial Focus">
// Copyright (c) Spatial Focus. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo.Entities
{
public class Review
{
public ReviewRating Rating { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// <copyright file="ReviewRating.cs" company="Spatial Focus">
// Copyright (c) Spatial Focus. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo.Entities
{
public enum ReviewRating
{
Excellent = 1,

Average,

Bad,
}
}
Loading

0 comments on commit fa2a51d

Please sign in to comment.