Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 895
William Avatar asked Apr 07 '10 02:04

William


1 Answers

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.

like image 141
Samuel Meacham Avatar answered Oct 21 '22 21:10

Samuel Meacham