Inheritance EF Code-First

前端 未结 1 1084
一个人的身影
一个人的身影 2021-02-15 01:36

I have a base object that I dont want to be mapped in DB as an entity, I only want the properties to be added to the object that is mapped in the DB :

Not mapped object

相关标签:
1条回答
  • 2021-02-15 02:13

    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");
        });            
    }
    
    0 讨论(0)
提交回复
热议问题