locked
EF 4.1 Code First and TPC Inheritance Mapping RRS feed

  • Question

  • It seems that EF 4.1 RTW creates a separate table even for the abstract base class in the hierarchy in a TPC mapping scenario. The following object model which has been taken from this blog post created a correct schema at the time of CTP5 build but now with the EF 4.1 RTW, the generated Schema has one table for the BillingDetail abstract class and I am not sure how I can stop Code First to create a table for the abstract class in the model. 

    public abstract class BillingDetail
    {
     public int BillingDetailId { get; set; }
     public string Owner { get; set; }
     public string Number { get; set; }
    }
     
    public class BankAccount : BillingDetail
    {
     public string BankName { get; set; }
     public string Swift { get; set; }
    }
     
    public class CreditCard : BillingDetail
    {
     public int CardType { get; set; }
     public string ExpiryMonth { get; set; }
     public string ExpiryYear { get; set; }
    }
     
    public class InheritanceMappingContext : DbContext
    {
     public DbSet<BillingDetail> BillingDetails { get; set; }
     
     protected override void OnModelCreating(ModelBuilder modelBuilder)
     {
     modelBuilder.Entity<BankAccount>().Map(m =>
     {
      m.MapInheritedProperties();
      m.ToTable("BankAccounts");
     });
     
     modelBuilder.Entity<CreditCard>().Map(m =>
     {
      m.MapInheritedProperties();
      m.ToTable("CreditCards");
     });  
     }
    }
    

    I would greatly appreciate if someone could clarify this.

    Thanks,

    Morteza

     



    Tuesday, April 19, 2011 9:10 PM

Answers

  • Hi Morteza and Miguel,

    What you are describing seems to match a an issue we found very late in EF 4.1 RTW cycle and we decided to postpone because our evaluation indicated that it had very low impact: while this unnecessary table is created for an abstract base type, the table does not participate in any mapping, therefore it shouldn't appear in any query produced by EF.

    For case in which you are using Code First to generate the database, the table will be added to the database schema but never used. For cases in which you are using the Code First API to map to an existing database it shouldn't have an impact either, since no queries generated by EF will every fail because the table is not there.

    If you are seeing other behavior or higher impact, please let us know.

    By the way, I am not aware of a workaround tha just removes the table without changing the shape of your model.

    Hope this helps,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, April 20, 2011 8:11 PM

All replies

  • Hello, I have the same problem, and the code you provide works perfectly in EF 4.1 CT5/RC.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bbb13bd7-1f20-41e6-bf18-743ac412b4ec

    If you find the fix please report me, I'll do the same.

    Thanks,

    Miguel.

    Wednesday, April 20, 2011 1:09 AM
  • Hi Morteza and Miguel,

    What you are describing seems to match a an issue we found very late in EF 4.1 RTW cycle and we decided to postpone because our evaluation indicated that it had very low impact: while this unnecessary table is created for an abstract base type, the table does not participate in any mapping, therefore it shouldn't appear in any query produced by EF.

    For case in which you are using Code First to generate the database, the table will be added to the database schema but never used. For cases in which you are using the Code First API to map to an existing database it shouldn't have an impact either, since no queries generated by EF will every fail because the table is not there.

    If you are seeing other behavior or higher impact, please let us know.

    By the way, I am not aware of a workaround tha just removes the table without changing the shape of your model.

    Hope this helps,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, April 20, 2011 8:11 PM
  • Hi Diego,

    Thank you very much for taking the time to answer this question, I really appreciate that.

    I totally agree with you, it's not a big deal since this table is just seating there and does not participate in queries generated by EF. My test shows that we can even delete this table from the schema and Code First will still work with it without any problem which is awesome, so this could be the workaround for the time being :)

    Thanks a lot,

    Morteza

     


    Thursday, April 21, 2011 2:05 AM