Inheritance with.Include in Entity Framework?

entity-framework

Question

I believe there is a somewhat related, but not precisely same, post about this here.

Let's name the two entities I have in my EF model Person and Developer. The latter inherits from the former.

Additionally, I belong to a group on Developer called Qualifications. The Person entity cannot see this.

How do I automatically whether I'm creating a query against the context. Include () the developer's credentials, such as

in context from the employee. Employee\s. Include ("Qualifications") choose an employee

fails to function... Since there is just one Employee object in the context, EF complains that the relationship does not exist (I suppose this is because it does not exist on Employee).

1
9
9/22/2009 5:32:01 PM

Popular Answer

I've run across this issue and done some testing. Here's what I discovered to work with LINQ 2 Entity.

Imagine we have Person - Developer - Qualifications in your example.

You would do this if you wanted to choose a developer who met your requirements.

var dev = (from d in context.Persons.OfType<Developer>
                            .Include("Qualifications")
          where d.ID == id
          select d).FirstOfDefault();

Let's imagine we have more associations between Person and Address. Using LINQ 2 Entity, we can additionally select Address.

var dev = (from d in context.Persons
                            .Include("Address")
                            .OfType<Developer>()
                            .Include("Qualifications")
          where d.ID == id
          select d).FirstOfDefault();

Observe how I made sure to include everything I required both before and after converting the Type. Now there should be no issues integrating the two inclusions. Both of these approaches pass my tests and are effective. Given that you have your inheritance configured properly, I hope these work for you.

GL.

12
6/16/2016 12:34:09 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow