views:

129

answers:

2

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

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.

Doobi
+2  A: 

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.

Samuel Meacham
+1 but mostly because of your blog post that I read months ago.:) I had a TPT implementation (30+ classes) that we're paying the price for now. Your blog post made me know it wasn't just me. I now work around it going forward.
itchi