none
Entity Framework Code First with TPH inheritting a foreign key adds additional columns in Database table RRS feed

  • Question

  • Hi, I am facing a problem with EF Code First with TPH. I am getting two additional columns in the table which is not in the model.

    I have a class structure as following :

       public class TestBase
        {
            public int Id { get; set; }


            public string Name { get; set; }
            [ForeignKey("TestMaster")]
            public string TestMaster_Id { get; set; }

            public TestMaster TestMaster { get; set; }

        }

        public class TestInherit1 : TestBase
        {
            public string Val1 { get; set; }
        }

        public class TestInherit2 : TestBase
        {
            public string  val2 { get; set; }
        }

        public class TestMaster
        {
            [Key]
            public string Id { get; set; }

            public virtual ICollection<TestInherit1> Inherit1List { get; set; }
            public virtual ICollection<TestInherit2> Inherit2List { get; set; }
        }

    In DBContext I have the following :

            public DbSet<TestMaster> TestMaster
            {
                get;
                set;
            }

            public DbSet<TestBase> TestBase
            {
                get;
                set;
            }

            [NotMapped]
            public DbSet<TestInherit1> TestInherit1
            {
                get;
                set;
            }

            [NotMapped]
            public DbSet<TestInherit2> TestInherit2
            {
                get;
                set;
            }

    The following  is the EF generated tables structure:

    [TestBases]

        [Id] [int] IDENTITY(1,1) NOT NULL,
        [Name] [nvarchar](max) NULL,
        [TestMaster_Id] [nvarchar](128) NULL,
        [Val1] [nvarchar](max) NULL,
        [val2] [nvarchar](max) NULL,
        [Discriminator] [nvarchar](128) NOT NULL,
        [TestMaster_Id1] [nvarchar](128) NULL,
        [TestMaster_Id2] [nvarchar](128) NULL

    [TestMasters](
        [Id] [nvarchar](128) NOT NULL

    If you look at the TestBases table, there are two additional columns TestMaster_id1 and TestMaster_Id2 being generated , that is not defined in the class. EF is inserting those two columns . I already have the foreign key defined on the column TestMaster_Id. What am I doing wrong?. I do not want these two columns.

    Monday, July 2, 2012 9:15 AM

Answers

  • Hi AshJh,

    Welcome to MSDN Forum.

    This is because you create two navigation properties in 'TestMaster' entity, the correct way is create a 'TestBase' navigation property. When you wan to access 'TestInherit1' or 'TestInherit2', using OfType method. Please refer to the code below, it will create the expected database.

    class Program
        {
            static void Main(string[] args)
            {
                using (Context context = new Context())
                {
                    context.Database.Create();
                }
            }
        }
    
        public class TestBase
        {
            public int Id { get; set; }
    
    
            public string Name { get; set; }
            public TestMaster TestMaster { get; set; }
    
        }
    
        public class TestInherit1 : TestBase
        {
            public string Val1 { get; set; }
        }
    
        public class TestInherit2 : TestBase
        {
            public string val2 { get; set; }
        }
    
        public class TestMaster
        {
            [Key]
            public string Id { get; set; }
    
            public virtual ICollection<TestBase> Inherit1List { get; set; }       
        }
    
        public class Context : DbContext
        {
            public DbSet<TestMaster> TestMaster
            {
                get;
                set;
            }
    
            public DbSet<TestBase> TestBase
            {
                get;
                set;
            }
    
        }

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, July 4, 2012 7:39 AM
    Moderator