C# EntityFramework 5代码优先多重继承映射(TPC)
好吧,也许这个问题以前已经回答过了,但我一直在研究,我就是找不到解决我具体问题的方法 所以我有一个EntityFramework代码优先模型,它有多个继承对象。我创建了这个代表我的问题的示例: 模型 上下文 应用迁移并更新数据库后,这是数据库模型: WHYYYYYYYY? 我错过了什么?我只想在继承层次结构中映射我的引用属性。我希望C# EntityFramework 5代码优先多重继承映射(TPC),c#,entity-framework,ef-code-first,entity-framework-5,entity-framework-mapping,C#,Entity Framework,Ef Code First,Entity Framework 5,Entity Framework Mapping,好吧,也许这个问题以前已经回答过了,但我一直在研究,我就是找不到解决我具体问题的方法 所以我有一个EntityFramework代码优先模型,它有多个继承对象。我创建了这个代表我的问题的示例: 模型 上下文 应用迁移并更新数据库后,这是数据库模型: WHYYYYYYYY? 我错过了什么?我只想在继承层次结构中映射我的引用属性。我希望ExecutiveEmployees表包含一个指向Genders的外键,与Employees和Genders完全相同 我在我的MyContext.OnModelC
ExecutiveEmployees
表包含一个指向Genders
的外键,与Employees
和Genders
完全相同
我在我的MyContext.OnModelCreating上尝试了这个:
modelBuilder.Entity<ExecutiveEmployee>().Map(x =>
{
x.MapInheritedProperties();
x.Properties(c => c.Gender);// <<-- does not work
x.ToTable("ExecutiveEmployees");
});
modelBuilder.Entity().Map(x=>
{
x、 MapInheritedProperties();
x、 属性(c=>c.Gender);//这很奇怪,我将您的示例运行到visual studio,并使用EF Power Tools查看EDMX生成器如何可视化这些关系,这就是我得到的:
从这个图中,我可以看出为什么会出错,因为现在实体框架假设导航属性已经在父类中找到。
至于如何操作,我认为这是一个关于TPC代码中多级继承的错误,应该先修复。有道理,我也这么想。因为EF映射的是我的值类型,而不是我的引用类型,所以这种行为让我认为这是一个错误=((我在EF中发布了一个错误:我从EF错误报告中读取了信息:“但是,如果使用FK关联,则可以映射这种类型的关系。”-因此,如果您将Person类中的[ForeignKey]字段添加到Gender,它应该会起作用(如果我理解正确的话)?您尝试过他们的解决方案吗?
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");
});
}
}
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; }
....
}
modelBuilder.Entity<ExecutiveEmployee>().Map(x =>
{
x.MapInheritedProperties();
x.Properties(c => c.Gender);// <<-- does not work
x.ToTable("ExecutiveEmployees");
});