Closed ganaware closed 2 years ago
Duplicate of #9148
Thank you. I understand that it's my bug.
I fixed as follows:
public class Book {
...
public static void OnModelCreating(ModelBuilder modelBuilder) {
var e_tb = modelBuilder.Entity<Book>();
e_tb.Property(e => e.BookId);
e_tb.OwnsOne(e => e.EnglishInfo, cb => Info.OnModelCreating(cb));
e_tb.OwnsOne(e => e.JapaneseInfo, cb => Info.OnModelCreating(cb));
e_tb.OwnsOne(e => e.ChineseInfo, cb => Info.OnModelCreating(cb));
}
}
public class Info {
...
public static void OnModelCreating<T>(ReferenceOwnershipBuilder<T, Info> rob) where T : class {
rob.Property(e => e.Title);
rob.Property(e => e.Author)
.HasMaxLength(100);
rob.Property(e => e.Memo);
}
}
public class MyContext : DbContext {
...
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
//Info.OnModelCreating(modelBuilder);
Book.OnModelCreating(modelBuilder);
}
}
Everything works as expected.
Fixed project is here: https://github.com/ganaware/TestEFCoreOwned/tree/ProblemSolved
Only the 1st Owned Entity property can be configured by Fluent API
Steps to reproduce
A complete project is here: https://github.com/ganaware/TestEFCoreOwned
I defined an entity Book that has three Owned Entity properties: EnglishInfo, JapaneseInfo and ChineseInfo. The type of these properties is Info configured by fluend API.
Then, I created a migration by dotnet command:
The generated 20181206004616_InitialCreate.cs is:
The problem is:
In short, Only the 1st Owned Entity property (EnglishInfo) can be configured by Fluent API. Configurations for 2nd (JapaneseInfo) and 3rd (ChineseInfo) are ignored.
Further technical details