TPH和知识库 [英] TPH and the Repository
问题描述
假设我有一个模型,我有联系人,人和公司。人和公司都来自联系人。
Let's say i have a model where I have Contact, Person and Company. Person and Company both derive from Contact.
在我的存储库中,我试图有三个公开的集合,如下所示:
On my repository, i am trying to have three exposed collections like this :
public DbSet<Contact> Contacts { get; set; }
public DbSet<Person> Persons { get; set; }
public DbSet<Company> Companies { get; set; }
此外也是这样的映射对于TPH:
Also mapping like this for a TPH:
modelBuilder.Entity<Contact>().Map<Person>(map=>map.MapInheritedProperties());
modelBuilder.Entity<Contact>().Map<Company>(map => map.MapInheritedProperties());
生成的数据库模式生成三个表,两个只有和ID,一个Discrimantaor和Contact表......这是应该发生的吗?
The generated database schema generates three tables, two with only and ID and a Discrimantaor and the the Contact table... Is this what is supposed to be happening ?
推荐答案
我在想,Contact类可能是抽象的?如果是,那么你可以尝试这个来获得TPH
I am thinking Contact class is probably abstract? If it is then u can try this to get TPH
modelBuilder.Entity< Contact>()。ToTable(" Contacts");
modelBuilder.Entity<Contact>().ToTable("Contacts");
&NBSP; &NBSP; &NBSP; &NBSP; modelBuilder.Entity< Person>()。Map(mc => mc.Requires(" Type")。HasValue(" Person"));
modelBuilder.Entity<Person>().Map(mc => mc.Requires("Type").HasValue("Person"));
; &NBSP; &NBSP; &NBSP; &NBSP; modelBuilder.Entity< Company>()。Map(mc => mc.Requires(" Type")。HasValue(" Company"));
modelBuilder.Entity<Company>().Map(mc => mc.Requires("Type").HasValue("Company"));
这篇关于TPH和知识库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!