Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework 5 Code First multiple inheritance mapping (TPC)

All right, probably this question has been answered before but I have been researching and I just can not find the solution to my specific problem

Code of this sample - Visual Studio 2012 - Console App

So I have an EntityFramework Code First model with multiple inheritance objects. I created this example representing my problem:

Model

public abstract class Person
{
    [Key]
    public Guid PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Employee : Person
{
    public decimal BaseSalary { get; set; }
}

public class ExecutiveEmployee : Employee
{
    public string Title { get; set; }
}

Context

public class MyContext : DbContext
{
    public DbSet<Employee> Employees { get; set; }
    public DbSet<ExecutiveEmployee> ExecutiveEmployees { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Employee>().Map(x =>
            {
                x.MapInheritedProperties();
                x.ToTable("Employees");
            });

        modelBuilder.Entity<ExecutiveEmployee>().Map(x =>
            {
                x.MapInheritedProperties();
                x.ToTable("ExecutiveEmployees");
            });
    }
}

I want to use TPC (Table Per Concrete Type) mapping

After running the migration and updating my database this is the result:

enter image description here

This was my expectation. So far so good.

But then....... I decided to add a Gender object and a property to my Person class like this: (This is not my real model it's just an example)

public class Gender
{
    [Key]
    public Guid GenderId { get; set; }

    [Required]
    [MaxLength(250)]
    public string Name { get; set; }
}

public abstract class Person
{
    ....
    public Gender Gender { get; set; }
    ....
}

public class MyContext : DbContext
{
    ....
    public DbSet<Gender> Genders { get; set; }
    ....
 }

After applying the migration and updating the database this is the database model:

enter image description here

WHYYYYYYYY?

What am I missing? I just want EF to map my reference properties in my inheritance hierarchy. I'm expecting that the ExecutiveEmployees table contain a foreign key to Genders exactly as Employees and Genders

I tried this on my MyContext.OnModelCreating:

modelBuilder.Entity<ExecutiveEmployee>().Map(x =>
    {
        x.MapInheritedProperties();
        x.Properties(c => c.Gender);// <<-- does not work
        x.ToTable("ExecutiveEmployees");
    });

But when I try to add the migration I receive this error:

The property 'Gender' on type 'ExecutiveEmployee' cannot be mapped because it has been explicitly excluded from the model or it is of a type not supported by the DbModelBuilderVersion being used.

like image 681
Jupaol Avatar asked Mar 15 '13 19:03

Jupaol


1 Answers

Well this is weird, i ran your sample into visual studio and used EF Power Tools to see how the EDMX generator is visualizing these relationships and this is what i got: Entity Data Model
From this diagram i can see why this is going wrong since now entity framework is assuming that the navigation property is already found in the parent class. Now as to how, i think this is a bug concerning multi-level inheritance in Code First with TPC and should be fixed.

like image 64
Ibrahim Najjar Avatar answered Sep 20 '22 10:09

Ibrahim Najjar