8

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.

Jupaol
  • 21,107
  • 8
  • 68
  • 100
  • I posted a bug in EF: https://entityframework.codeplex.com/workitem/1049 – Jupaol Apr 18 '13 at 14:34
  • I read info from EF bug report: "However, it is possible to map this type of relationship if an FK association is used." - so if You would add [ForeignKey] field in Person class to Gender it should work (if I understand that correctly)? Have You tried their solution? – Prokurors Oct 26 '13 at 13:37

1 Answers1

2

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.

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
  • Makes sense, I was thinking the same. Since EF is mapping my Value Types but not my Reference Types, this behavior made me think this was a bug =(( – Jupaol Mar 18 '13 at 14:50