Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8173739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:15:59+00:00 2026-06-06T22:15:59+00:00

I am facing a problem with EF Code First with TPH. I am getting

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:16:02+00:00Added an answer on June 6, 2026 at 10:16 pm

    Your table contains more undefined columns: Val1, Val2 and Discriminator should also not be mapped if you don’t want to map derived types. But if you don’t map those types you will never get those instances from EF and you will never be able to store that instances to EF.

    Your NotMapped attribute is used incorrectly. It must be declared either on entity type or on property inside entity type. Also if you don’t want to map derived types you should not create DbSet properties for them.

    The correct usage of the attribute is either:

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

    Or:

    [NotMapped]
    public virtual ICollection<TestInherit1> Inherit1List { get; set; }
    

    At the moment your entities are mapped and that is also a reason why you get two new foreign keys – you defined navigation properties to derived types. Each of them needs its own foreign key because EF is not able to redefine mapping of parent properties in derived class.

    If you want only single foreign key declared in the parent entity you must define only single navigation property to the parent:

    public virtual ICollection<TestBase> TestList { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am facing the problem tapping on the table view my code was not
i am using mvc 3 code first. facing problem while passing data form SecurityAttribute
Hi I am facing a problem in Message dialog, getting Force close my code
hi im new to jquery and im facing these problem i have these code
I have facing problem in listview i am getting data from server and after
I have following HTML code: <div id=wrapper> <div class=page id=first> Test<br/> Test<br/> Test<br/> Test<br/>
I'm facing a little problem with my jQuery / Javascript code. I have several
Iam facing problem in understanding and converting a matlab code into opencv. I want
iam facing problem in passing array to view. this is my controller code.. function
I'm currently facing new problem with operators. Using following code, I want to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.