Conditional mapping with TPH (Table-per-heirarchy) not applying? RRS feed

  • Question

  • I have a master reference table. I want to inherit subsets of this table based on simple conditions.

    I've got it working using TPH, but a weird thing happens - if I only have a single table inheriting, then the condition isn't applied at all. The following test demonstrates this:

    1) Inherit from abstract ParentEntity - ChildEntityA (with condition FieldValue = 5), and ChildEntityB (with condition FieldValue = 17)

    Works CORRECTLY - Looping ChildEntityA only lists relevant records where FieldValue = 5. (ChildEntityB also works fine.)

    2) Delete ChildEntityB from model

    Works INCORRECTLY - Looping ChildEntityA now lists ALL records in ParentTable regardless of FieldValue (!!??)

    3) Add ChildEntityB to model again with condition FieldValue = 17

    Works CORRECTLY - both entities list their respective records again

    It's as if EF only bothers to apply the condition if there are multiple children inheriting. 

    Known problem? "Feature"?



    Thursday, November 29, 2012 5:57 AM


  • Hi Brendan,

    Welcome to the MSDN forum.

    Could you please let me know the code snippet about entity class and context class?

    I think the condition you mentioned is the Discriminator which is generated by Entity Framework automatically. Did you create this property manually?

    Here is a simple example about TPH you could check:

        public abstract class ParentEntity
            public Int32 Id { get; set; }
            public String Info { get; set; }
        public class ChildEntityA : ParentEntity
            public String InfoA { get; set; }
        public class ChildEntityB : ParentEntity
            public String InfoB { get; set; }
        public class tstContext : DbContext
            public DbSet<ParentEntity> ParentEntities { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
                modelBuilder.Entity<ParentEntity>().Map<ChildEntityA>(m => m.Requires("ParentEntityType").HasValue(5)).Map<ChildEntityB>(m => m.Requires("ParentEntityType").HasValue(17));

    Have a nice day.

    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Friday, December 7, 2012 1:25 AM
    Friday, November 30, 2012 5:58 AM