[英]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 本示例的代码-Visual Studio 2012-控制台应用
So I have an EntityFramework Code First model with multiple inheritance objects. 所以我有一个带有多个继承对象的EntityFramework Code First模型。 I created this example representing my problem:
我创建了一个代表我的问题的示例:
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; }
}
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 我想使用TPC(每个具体类型的表格)映射
After running the migration and updating my database this is the result: 运行迁移并更新数据库后,结果如下:
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) 但是……。我决定像这样向我的
Person
类添加一个Gender
对象和一个属性:(这不是我的真实模型,它只是一个示例)
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: 应用迁移并更新数据库后,这就是数据库模型:
WHYYYYYYYY? WHYYYYYYYY?
What am I missing? 我想念什么? I just want EF to map my reference properties in my inheritance hierarchy.
我只希望EF在继承层次结构中映射我的引用属性。 I'm expecting that the
ExecutiveEmployees
table contain a foreign key to Genders
exactly as Employees
and Genders
我期待的是,
ExecutiveEmployees
表包含一个外键Genders
正是因为Employees
和Genders
I tried this on my MyContext.OnModelCreating
: 我在
MyContext.OnModelCreating
上尝试过此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.
无法映射类型“ ExecutiveEmployee”上的属性“ Gender”,因为该属性已从模型中明确排除,或者所使用的DbModelBuilderVersion不支持该类型。
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: 好吧,这很奇怪,我将您的示例放入了Visual Studio,并使用EF Power Tools来查看EDMX生成器如何可视化这些关系,这就是我得到的:
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.
现在,关于方式,我认为这是一个涉及TPC的Code First中的多级继承的错误,应予以修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.