问题是,尽管我们进行了修改(使用了每个类的表),子级的PK还是父键。
public class Entity
{
[Key]
public Guid EntityId { get; set; }
}
public class VersionedEntity: Entity
{
public Guid VersionId { get; set; }
}OnModelCreating包含:
modelBuilder.Entity<VersionedEntity>().Map(m =>
{
m.MapInheritedProperties();
});
modelBuilder.Entity<VersionedEntity>().HasKey(e => new { e.EntityId , e.HistoryId});因此,我们将得到仍然包含一列PK (EntityId)的EntityId。
我们是否有可能在m.MapInheritedProperties()替换/删除“继承”父键映射之后?
发布于 2015-02-07 14:59:20
不,没有。原因是EF对整个继承树有一个实体键定义。在我看来,它甚至应该为第二个HasKey语句抛出一个错误,而不是默默地忽略它。
拥有一个实体密钥定义的原因是,您可能(但可能不会)通过一个DbSet<Entity>公开所有实体。如果是这样的话,应该可以.
context.Entities.Find(Guid.Parse(someGuid));如果一个子类型有不同类型的键,这是不可能的。
如果您需要保留此模型,则最好让EF忽略基类,并分别映射每个类型,而不需要映射继承。在这个场景中有一个EntityTypeConfiguration可能会有所帮助。
https://stackoverflow.com/questions/28372870
复制相似问题