首先,支持继承。但在这个特定的例子中,它似乎并不像你所期望的那样。
由于关系数据库不支持继承(正如我们从面向对象编程中了解到的那样),因此必须进行某种转换才能实现继承。
以下是详细介绍该问题的一系列博客文章:
- 每个层次结构的表 http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx
- 每种类型的表 http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx
- 每班表 http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx
它还试图给出何时使用哪些策略的指导。
UPDATE
显然这比乍一看更棘手。您看到的很可能是由于循环引用造成的:A -> B -> Customer -> Bs
.
The CustomerID
Bs/C 的列不是从 As 表继承的列。它实际上是指定的关系属性的表示Customer
class:
public virtual ICollection<B> Bs { get; set; }
结果是nullable CustomerID
表 B 上的列。
public virtual ICollection<C> Cs { get; set; }
结果是nullable CustomerID
表 C 上的列。
所以那些可为空的列用于表示关系Customer -> Bs
and Customer -> Cs
。他们的外表与Customer
财产在A
class.
您可以通过删除客户类的导航属性来轻松检查这一点。那么结果就是你所期望的:ACustomerID
A表上的列,没有CustomerID
B/C 表上的列。
所以为了解决这个问题你需要专门告诉EF如何解决循环引用。但不确定这是否可能,恐怕您需要省略 Bs/Cs 属性Customer
并编写 LINQ 查询来检索信息。
如果您需要这些属性Customer
你可以做的课程是这样的:
public class Customer
{
public int ID { get; set; }
// this is necessary to have access to the related Bs/Cs
// also it cant be private otherwise EF will not overload it properly
public virtual ICollection<A> As { get; set; }
public IEnumerable<B> Bs { get { return this.As.OfType<B>(); } }
public IEnumerable<C> Cs { get { return this.As.OfType<C>(); } }
}