LukeHennerley
LukeHennerley

Reputation: 6444

2 POCO's with inheritance creates one table instead of two

I am creating a fairly simplistic Database for a holiday system.

Entities

Employee

public class Employee
{
  public int Id { get; set;}
  public string Username { get; set; }
  public LineManger Manager { get; set; }
}

LineManger

public class LineManager : Employee
{
  public string CompanySection { get; set; }
}

Relationship

As you can see, each employee has one LineManger however an Employee could be a LineManager.

In my DbContext I have:

public DbSet<Employee> Employees { get; set; }
public DbSet<LineManager> Managers { get; set; }
public DbSet<HolidayConfiguration> Configurations { get; set; }
public DbSet<Holiday> Holidays { get; set; }

When I ran PM > Update-Database -verbose for the first time it only created an Employee table and this table has CompanySection and Manager_Id which doesn't map to anywhere seeing as there isn't a LineManager table.

Any explanations on why this has happened? Is this correct?

Upvotes: 0

Views: 120

Answers (1)

Piotr Stapp
Piotr Stapp

Reputation: 19830

It has to be like this, because every LineManager is an Employee, so when you are querying Employees you are also looking for LineManager. DbSet<Employee> Employees matches only one table, so the only way how EF can create table is to put in it all fields from LineManager.

Sometime (when you have more inhertiance) EF create special column in which it puts class name.

Upvotes: 1

Related Questions