我正在使用Entity Framework v4。我创建了一个POCO类,其中包含一堆标量属性和一个返回Interface类型的集合。如何在EF模型中建立这种关系?如何显示包含不同项目但它们都有一个公共(public)界面的集合?这是我要实现的目标的一个示例。

interface IPatientDocument{}
public class Lab : IPatientDocument{.....}
public class Encounter : IPatientDocument{...}
public class MedicationLog : IPatientDocument{...}

//Incomplete class listing
//Once I have aggregated the different doc types, I can then use Linq to Obj to retrieve the specific doc type I need.  Currently I have about 26 doc types and do not want to create a collection for each one
public class Patient
{
   IList<IPatientDocument> DocumentCollection;
}

最佳答案

如果您在 Entity Framework 中使用TPT(按类型划分表)继承,请非常非常。特别是如果您有26种文档类型。 I did a blog post用于TPT继承的损坏的SQL生成,还有opened a bug on Microsoft Connect。 MS承认了这个问题,他们说他们正在努力,但不要屏住呼吸。他们花了3个月的时间来确认问题,他们的全部回答是:“我们知道TPT层次结构的性能问题。我们目前正在研究解决方案,并希望在将来的版本中对此领域进行改进”。

使用26种文档类型,即使使用基本查询,EF也会花费近2分钟的时间来生成SQL(将在8000行附近),并且SQL Server会处理它。您将在可笑的子查询中深入30个级别。不惜一切代价避免TPT继承。如果您只是启动一个应用程序,那么它似乎可以正常运行,因为您通常只有几个子类型,但是一旦添加了更多子类型,您的应用程序就会减慢抓取速度。

关于c# - 以接口(interface)作为返回类型的 Entity Framework 和建模集合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2589678/

10-14 18:03
查看更多