【发布时间】:2017-07-20 00:27:26
【问题描述】:
所以我已经实现了每个具体类的表来处理继承层次结构,但是我在导航属性方面遇到了问题。
我的模型结构如下:
我有一个抽象的 BaseEntity 类,有多个派生类,所以:
public abstract class BaseEntity
{
public virtual ICollection<Comment> Comments { get; set; }
public EntitytType EntitytType { get; set; }
}
public class FirstDerivedEntity : BaseEntity
{
//EntitytType == First;
}
public class SecondDerivedEntity : BaseEntity
{
//EntitytType == Second;
}
public class Comment
{
public long BaseEntityId { get; set; }
public EntitytType BaseEntityType { get; set; }
}
public enum EntitytType
{
First,
Second
}
这里的 Comments 导航属性不起作用,因为每个派生(具体)类都有自己的一组 Id。在我看来,每个表中的 EntityType 列都可以作为某种鉴别器,但我不知道如何告诉 EF 使用它。
任何帮助将不胜感激!
【问题讨论】:
-
你应该更具体。你想达到什么目的?
-
查看this 了解这个常见问题。
标签: c# inheritance entity-framework-6 foreign-keys table-per-class