简体   繁体   中英

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

I am using Entity Framework v4. I have created a POCO class that contains a bunch of scalar properties and a collection that returns an Interface type. How do I create this relationship in the EF model? 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. Especially if you have 26 doc types. I did a blog post on the broken SQL generation for TPT inheritance, and also opened a bug on Microsoft Connect . MS has admitted to the problem, and they say they are working on it, but don't hold your breath. 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".

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. You'll be 30 levels deep in ridiculous sub queries. Avoid TPT inheritance at all costs. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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