Entity Framework and Modeling Collections with an Interface as a Return Type

I am using Entity Framework v4. I created a POCO class that contains a bunch of scalar properties and a collection that returns an interface type. How to create this relation in the EF model? How to show a collection containing different elements, but they all have a common interface? Here is an example of what I'm 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; } 
+6
c # entity-framework poco
source share
2 answers

Be very very careful if you use TPT (Table-Per-Type) inheritance with the Entity Framework. Especially if you have 26 types of documents. I made a blog post about broken SQL generation for TPT inheritance, and also opened a bug in Microsoft Connect . MS admitted to the problem, and they say that they are working on it, but do not hold your breath. It took them 3 months to confirm this problem, and all they said was, “We know about performance issues with TPT hierarchies. We are currently studying solutions and expect improvements in this area in a future version.”

With 26 types of documents, even with a basic query, EF will take about 2 minutes to generate SQL (which will be in the region of 8000 lines) and SQL Server to process it. You will have 30 levels in funny subqueries. Avoid TPT inheritance at all costs. If you are just starting the application, it looks like it works because you usually only have a few subtypes, but as soon as you add more, your application will scan slowly.

+3
source share

I do not know if this is completely. If you could implement the base class, you would probably execute a table for each type of inheritance, which is allowed. But if you are just trying to select arbitrary types, you fall into co- and contra-variance, which may be worth exploring, since it is new in .NET 4.

0
source share

All Articles