[英]Does EF Core support an abstract base entity which is generic?
I remember there were problems with generic entities in previous EF. 我记得以前EF中的通用实体存在问题。 How about EF Core? EF Core怎么样? I can't find docs related to this matter. 我找不到与此事相关的文档。
For example: 例如:
public abstract class Parent<TEntity> {
public int EntityId { get; set; }
public TEntity Entity { get; set; }
}
public class Child : Parent<Foo> {
}
public class OtherChild : Parent<Bar> {
}
// config for child entities includes this:
config.HasKey(c => c.EntityId);
Though this throws stating that child entities do not define a primary key, when they clearly do! 虽然这会引发说明子实体没有定义主键,但是当他们显然这样做时!
I can fix this by making Parent
non-generic. 我可以通过使Parent
非泛型来解决这个问题。
Are there official docs for this? 有官方文件吗? Am I doing something wrong, or is this the expected behavior? 我做错了什么,或者这是预期的行为?
I can use this model in ef-core 1.1.0: 我可以在ef-core 1.1.0中使用这个模型:
public abstract class Parent<TEntity>
{
public int EntityId { get; set; }
public TEntity Entity { get; set; }
}
public class Child : Parent<Foo>
{
}
public class OtherChild : Parent<Bar>
{
}
public class Foo
{
public int Id { get; set; }
}
public class Bar
{
public int Id { get; set; }
}
With this mapping in the context: 在上下文中使用此映射:
protected override void OnModelCreating(ModelBuilder mb)
{
mb.Entity<Child>().HasKey(a => a.EntityId);
mb.Entity<Child>().HasOne(c => c.Entity).WithMany().HasForeignKey("ParentId");
mb.Entity<OtherChild>().HasKey(a => a.EntityId);
mb.Entity<OtherChild>().HasOne(c => c.Entity).WithMany().HasForeignKey("ParentId");
}
Which leads to this fantastic model: 这导致了这个梦幻般的模型:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.