问题
I have an abstract base class called Party. There are several concrete subclasses (Company, Person, Department). Party has a property called PartyType which is use as the discriminator. Each type is in its own table with configurations like
Map<Person>(p => p.Requires("PartyType").HasValue("Person").ToTable("People");
Everything works well. Now I want to add a subclass of Person called Employee. How do I map this? I've tried
Map<Employee>(e => e.Requires("PartyType").HasValue("Employee")
.ToTable("Employees");
but this gives a runtime error of
(43,10) : error 3032: Problem in mapping fragments starting at lines 43, 84:EntityTypes WOL.EFData.Person, WOL.EFData.Employee are being mapped to the same rows in table People. Mapping conditions can be used to distinguish the rows that these types are mapped to.
回答1:
In table per type mapping EF does not expect a discriminator configuration.
modelBuilder.Entity<Person>().ToTable("People");
modelBuilder.Entity<Employee>().ToTable("Employees");
See this article for more information.
来源:https://stackoverflow.com/questions/11148534/ef-4-3-1-how-to-map-a-sub-sub-class-with-table-per-type