Inheritance EF Code-First

梦想与她 提交于 2019-12-03 12:37:10

Morteza Manavi has a blog post detailing how to do this:

http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx

Basically, you'll need to override OnModelCreating in your DbContext and call MapInheritedProperties() for each of the child tables.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<BankAccount>().Map(m =>
    {
        m.MapInheritedProperties();
        m.ToTable("BankAccounts");
    });

    modelBuilder.Entity<CreditCard>().Map(m =>
    {
        m.MapInheritedProperties();
        m.ToTable("CreditCards");
    });            
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!