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

  • Home
  • SEARCH
  • 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 4247166
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:04:17+00:00 2026-05-21T04:04:17+00:00

I am trying to write a project using the code-first approach and I have

  • 0

I am trying to write a project using the code-first approach and I have run into the following problem

    public class BaseType
{
    [Key]
    public int id { get; set; }
    public string description { get; set; }
}
public class Type1 : BaseType
{
    public decimal price { get; set; }

}
public class mycontext : DbContext 
{
    public DbSet<BaseType> basetypes { get; set; }
    public DbSet<Type1> type1 { get; set; }

}

when I run the application and I create an object Type1 and use mycontext.type1.ADD(mytype1object); and I look at the database the table for Type one has the correct field but the parent table “basetypes” also has a price field.

Do I have to ignore the field explicitly?

Any suggestions?

  • 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-05-21T04:04:18+00:00Added an answer on May 21, 2026 at 4:04 am

    By default code first will use TPH (Table Per Hierarchy) inheritance. What this means is that both of your types are being stored in a single table called ‘BaseTypes’. You’ll notice that it also includes an extra field called ‘Discriminator’. EF will store a value in that field to designate which type that each record is.

    If you would like your types to be in different tables you’d need to setup your context for TPT (Table Per Type). You can decorate your classes with a Data Annotation with the table name, or you could use the model binder. Below is the data annotation way.

    [Table("BaseTypes")]
    public class BaseType
    {
        [Key]
        public int id { get; set; }
        public string description { get; set; }
    }
    
    [Table("Type1s")]
    public class Type1 : BaseType
    {
        public decimal price { get; set; }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write my first WCF project. I have created a project
I have the following code in a project that write 's the ascii representation
I am trying to write some unit tests for my MVC3 project (the first
I'm trying to write code in C++ (using template) to add between 2 matrices.
i'm trying to write a simple code for my project if user types walk
Hello I'm trying to write some tool using code::blocks, wxWidgets and libxml2 on Windows
I have the following code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
I am trying to write an Setup Project/Installer for a class library driver that
I'm trying to write a small chunk of code to grab the backbuffer into
I am trying to write some unit tests for my code. In out project

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.