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 6015039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:46:43+00:00 2026-05-23T02:46:43+00:00

Confusing Situation I have a situation where I have 2 entities where 1 inherits

  • 0

Confusing Situation

I have a situation where I have 2 entities where 1 inherits from the other, that need to map to 2 separate tables, but code use should be around the base of the 2 entities.

Details

public class Team
{
  public virtual int Id { get; set; }
  public virtual ICollection<Employee> Members { get; set; }
}
public class Employee
{
  public virtual int Id { get; set; }
  public virtual string Name { get; set; }
  public virtual ICollection<Team> Teams { get; set; }
}

public class EmployeeInfo : Employee
{
  public virtual int Id { get; set; }
  public virtual decimal Amount { get; set; }
}

We have an existing database schema where Employee and EmployeeInfo are separate tables with a FK between EmployeeInfo_Id and Employee_Id.

In our system “managers” will be adding Employee’s to the system, with a set of private information (more properties than listed above) like pay, and add them to a Team. Other areas of the system will be using the Team or Employee objects for various other things. We would like to have to code super simple if the mapping can be done.

When a manager creates a new employee we would like the code to look something like this:

public void Foo(string name, decimal pay)
{
  // create the employee
  var employee = new EmployeeInfo();
  employee.Name = name;
  employee.Pay = pay;

  // add him/her to the team
  _team.Employees.Add(employee);   // the idea being that consumers of the Team entity would not get the separate employee info properties

  // save the context
  _context.SaveChanges();
}

The end result would be that the EmployeeInfo properties entered into the EmployeeInfo table and the base Employee data is entered into the Employee table and added to the Team via the association table TeamEmployees.

So far I’m trying the current mappings, and I get an invalid column named “Discriminator.” When just adding an employee to a team.

public class TeamConfiguration : EntityTypeConfiguration<Team>
{
    public TeamConfiguration()
    {
        ToTable("Team");

        HasKey(t => t.Id);
        HasMany(t => t.Members).WithMany(m => m.Teams)
            .Map(m =>
                     {
                         m.MapLeftKey("Team_Id");
                         m.MapRightKey("Employee_Id");
                         m.ToTable("TeamEmployees");
                     });
    }
}

public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
    public EmployeeConfiguration()
    {
        ToTable("Employee");
        ToTable("EmployeeInfo");

        HasKey(t => t.Id);
        Property(p => p.Name);

        HasMany(m => m.Teams)
            .WithMany(t => t.Members)
            .Map(m =>
                     {
                         m.MapLeftKey("Employee_Id");
                         m.MapRightKey("Team_Id");
                         m.ToTable("TeamEmployees");
                     });
    }
}

Also, if I take the many-to-many between team and employee out of the mix I get a FK exception on Employee_Id to EmployeeInfo_Id.

Thanks, JR.

  • 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-23T02:46:43+00:00Added an answer on May 23, 2026 at 2:46 am

    Discriminator is a column that’s being added to your table when you use Table Per Hierarchy approach.
    I think what you’re looking for is “Table per Type (TPT)”. Decorate your EmployeeInfo class as follows:

    [Table("EmployeeInfo")]
    public class EmployeeInfo : Employee 
    

    Or add below to your OnModelCreating event:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
     ...
     modelBuilder.Entity<EmployeeInfo>().ToTable("EmployeeInfo");
     ...
    }  
    

    Or, create the following class and use it like modelBuilder.Configurations.Add(new EmployeeInfoConfiguration()); in OnModelCreating method:

    public class EmployeeInfoConfiguration : EntityTypeConfiguration<EmployeeInfo>
    {
        public EmployeeInfoConfiguration()
        {
            ToTable("EmployeeInfo");
        }
    }
    

    This will cause EF to create EmployeeInfo table with necessary constraints.

    Also, it’s good to initialize your collections in your objects’ constructors to prevent null exception. For example in Team class:

    public Team()
    {
        this.Employees = new HashSet<Employee>();
    }  
    

    I copied your code exactly, and changed the following parts:

    public class Team
    {
        public Team()
        {
            this.Members = new HashSet<Employee>();
        }
        public virtual int Id { get; set; }
        public virtual ICollection<Employee> Members { get; set; }
    }
    public class Employee
    {
        public Employee()
        {
         this.Teams = new HashSet<Team>();
        }
    
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual ICollection<Team> Teams { get; set; }
    }
    
    [Table("EmployeeInfo")]
    public class EmployeeInfo : Employee
    {
        public virtual int Id { get; set; }
        public virtual decimal Amount { get; set; }
    }  
    

    In the DbContext, no changes:

    public partial class TestEntities : DbContext
    {
        public DbSet<Employee> Employees { get; set; }
        public DbSet<EmployeeInfo> Employee_Info { get; set; }
        public DbSet<Team> Teams { get; set; }
    }  
    

    and your working Foo method:

    public static void Foo(string name, decimal pay)
    {
        var _team = new Team();
        var context = new TestEntities();
        context.Teams.Add(_team);
    
        // create the employee
        var employee = new EmployeeInfo();
        employee.Name = name;
        employee.Amount = pay;
        context.Employees.Add(employee);
        context.SaveChanges();
    
        // add him/her to the team
        _team.Members.Add(employee);   
    
        // save the context
        context.SaveChanges();
    }  
    

    Finally, remove ToTable("EmployeeInfo"); part from EmployeeConfiguration since you have mentioned this correctly in your mode creating event.

    For more info about Table Per Type approach, check out this great article.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a little confusing situation here when I use the Context from django.template.
I have what is, to me, a confusing situation. The following code is producing
I have a situation that is confusing me and was hoping for some help.
I have a situation where I need to ensure that there is only one
So, I have a confusing MySQL issue. I feel like I need to use
I have confusing situation. Base Generic Type and successor public abstract class BaseType<TEntity> :
I have run into a very confusing situation with maps on the iPhone. Everything
i have this situation a selecy: SELECT search.id FROM database.search WHERE search.talentnum > '0'
I have a very confusing problem, and I am able to solve it, but
I have a little bit of a confusing situation :) I have a JavaScript

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.