Using Code First with Multiple Inheritance Levels RRS feed

  • Question

  • Is it possible to have a multi-level inheritance model set up with Code First?

    Say I have the following classes:

    LiabilityRate and PropertyRate both inheriting from Rate with a TPT Model in place.

    But then I have
    BuildingRate and ContentsRate inheriting from PropertyRate with a TPH Model in place, discriminated based on a column on the PropertyRate table called RateType.


    The idea works in my head, I just can't seem to get it to work with Code First without errors.  My classes look like this:


      public abstract class Rate
        public int ID { get; set; }
        public DateTime EffectiveDate { get; set; }
        public DateTime? EndDate { get; set; }
      public class PropertyRate : Rate
        public int TerritoryID { get; set; }
        public int ProgramID { get; set; }
      public class LiabilityRate : Rate
        public int TerritoryID { get; set; }
      public class BuildingRate : PropertyRate
        public decimal Frame14 { get; set; }
        public decimal Frame58 { get; set; }
      public class ContentsRate : PropertyRate
        public decimal Frame14 { get; set; }
        public decimal Frame58 { get; set; }

    And my OnModelCreateing looks like:

        protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
            .Map(a => a.Requires("PropertyRateType").HasValue("BLDG"));
            .Map(a => a.Requires("PropertyRateType").HasValue("CNTS"));

    LiabilityRate would also be broken down further into Premises and Product Rates.




    I can only seem to get one of these Inheritance models to work at a time, when ever I try to combine them I end up with some error.  The one I'm currenly facing is:

    Schema specified is not valid. Errors: 

    (105,12) : error 2016: Member Discriminator specified in Condition does not exist.




    Wednesday, March 2, 2011 12:45 PM

All replies

  • Hi Jeff,

    CTP5 has a lot of bugs around hybrid mapping scenarios, so it really doesn't work. However we will shortly be releasing a Release Candidate which is a lot more stable around hybrid mapping and does support this scenario.

    Info about the upcoming releases is here; http://blogs.msdn.com/b/adonet/archive/2011/03/02/ef-4-1-is-coming-dbcontext-api-amp-code-first-rtw.aspx


    Wednesday, March 9, 2011 7:32 PM