Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Commented Issue: EntityFramework 5 Code First multiple inheritance mapping (Table Per Concrete Type) [1049]

$
0
0
#Steps to Reproduce#
---

##Model##

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

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

public abstract class Person
{
[Key]
public Guid PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Gender Gender { 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; }
public DbSet<Gender> Genders { 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");
});
}
}

#The current result#

When I generate the database model from this entities, this is what I get:

![Image](http://i.stack.imgur.com/J2yTa.png)

As you can see, EntityFramework is actually mapping the simple properties like strings, and Guids, but it's excluding the reference properties in my inheritance hierarchy

#My expectation#

I was expecting to have two tables in my database, (Employees and ExecutiveEmployees), __both with ALL the inherited members of the parent classes, including all the inherited reference type properties not just the value type properties__

#External links#

* [I asked this question in StackOverflow](http://stackoverflow.com/q/15440485/1268570)
* [Code of this sample - Visual Studio 2012 - Console App](http://sdrv.ms/WtfcVr)
Comments: After investigating this it turns out that the EF mapping engine does not allow mapping an independent association in this way. The problem is that that the mapping for the IA would need to switch on the type name, which is not supported and not possible to specify in MSL. (Note that even though the above example produces an EDMX and can generate a database, it fails at runtime as an invalid mapping.) However, it is possible to map this type of relationship if an FK association is used. For example, Adding a public Guid GenerId { get; set; } property to Person will allow this to work. This is because this FK property is mapped to columns just like any other property and does not need an association set mapping in the MSL. We have no plans to update the mapping engine to allow this for IAs given that it works fine with FKs and given that we would introduce shadow state with FK mappings before updating anything related to IAs. Therefore, closing this as won’t fix.

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>