简体   繁体   English

具有接口作为返回类型的实体框架和建模集合

[英]Entity Framework and Modeling Collections with an Interface as a return type

I am using Entity Framework v4. 我正在使用Entity Framework v4。 I have created a POCO class that contains a bunch of scalar properties and a collection that returns an Interface type. 我创建了一个包含一堆标量属性的POCO类和一个返回Interface类型的集合。 How do I create this relationship in the EF model? 如何在EF模型中创建此关系? How do I show a collection that contains different items but they all have a common interface? 如何显示包含不同项目的集合,但它们都具有通用界面? Here would be an example of what I am trying to achieve. 这将是我想要实现的一个例子。

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;
}

Be very very careful if you are using TPT (Table-Per-Type) inheritance with Entity Framework. 如果您在Entity Framework中使用TPT(Table-Per-Type)继承,请非常小心。 Especially if you have 26 doc types. 特别是如果你有26种doc类型。 I did a blog post on the broken SQL generation for TPT inheritance, and also opened a bug on Microsoft Connect . 我做了关于TPT继承的破坏SQL生成的博客文章 ,并且还在Microsoft Connect上打开了一个错误 MS has admitted to the problem, and they say they are working on it, but don't hold your breath. MS已经承认了这个问题,并且他们说他们正在研究它,但是不要屏住呼吸。 It took them 3 months to acknowledge the problem, and all they said was "We are aware of performance issues with TPT hierarchies. We are currently investigating solutions and expect to make improvements in this area in a future release". 他们花了3个月的时间来承认这个问题,他们所说的只是“我们意识到TPT层次结构的性能问题。我们目前正在调查解决方案,并期望在未来版本中对此领域进行改进”。

With 26 doc types, even with a basic query, it will take nearly 2 minutes for EF to generate the SQL (which will be in the neighborhood of 8000 lines), and for SQL Server to process it. 使用26种doc类型,即使使用基本查询,EF也需要将近2分钟来生成SQL(将在8000行附近),并且SQL Server需要处理它。 You'll be 30 levels deep in ridiculous sub queries. 在荒谬的子查询中你将达到30级。 Avoid TPT inheritance at all costs. 不惜一切代价避免TPT继承。 If you're just starting an app, it seems like it works, because you generally only have a few sub types, but once you add more, your application will slow to a crawl. 如果您刚刚启动应用程序,它似乎可以正常工作,因为您通常只有一些子类型,但是一旦添加更多,您的应用程序就会慢慢爬行。

I don't know if that's entirely legal. 我不知道这是否完全合法。 If you could implement using a base class, you'd probably doing table per type inheritance, which is allowed. 如果您可以使用基类实现,那么您可能正在为每个类型继承执行表,这是允许的。 But if you're just trying select arbitrary types you're getting into co- and contra variance, which might be worth investigating as it's new in .NET 4. 但是,如果您只是尝试选择任意类型,那么您将陷入共同和反向差异,这可能值得研究,因为它是.NET 4中的新功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM