1

Using a table-per-type inheritance model and Entity Framework Code First, I am trying to eager load a list of derived class. Please note that I can't change the model.

I have the following model (overly simplified)

public class Training
{
    public string Name { get; set; }
    public IList<Person> Persons { get; set; }
}
public abstract class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

[Table("Students")]
public class Student : Person
{
    public string StudentNumber { get; set; }
    public IList<Training> Trainings { get; set; }
}
[Table("Instructors")]
public class Instructor : Person
{
    public DateTime StartingDate { get; set; }
    public IList<Training> Trainings { get; set; }
}

I want to query Training by name and eager load all the persons including the derived class (Student and Instructor). Back in April 2011, Tom Dykstra seemed to claim it wasn't possible.

The current version of the Entity Framework doesn't support eager loading for one-to-zero-or-one relationships when the navigation property is on the derived class of a TPH inheritance structure.

Has this changed? I am using EF5.

1 Answer 1

2

I don't see why ...

var list = context.Trainings.Include(t => t.Persons)
    .Where(t => t.Name == someName)
    .ToList();

... shouldn't work. EF should populate the Persons list with concrete Student and Instructor entities.

You neither have a "one-to-zero-or-one relationship" nor is your navigation property (Training.Persons) "on the derived class". So, I think the mentioned limitation does not apply to your model and query.

Sign up to request clarification or add additional context in comments.

6 Comments

I think, what was meant by the navigation property on the derived class is the IList<Training> Trainings (that I forgot to add in my question initially) is inside the instructor and student class instead of the Person class. Isn't the relation between Person and Instructor/Student a one-to-zero-or-one relationship!?
As for the Include, I assumed you absolutely had to Include the Student & Instructor classes for the data to eager load for some reason. It works. Thanks!
@Nick-ACNB: Yes, it is a one-to-zero-or-one relationship on database schema level. But it's not an explicit relationship for EF with a reference navigation property (it is somehow "hidden" in the TPT mapping). I think the mentioned limitation only means such explicit relationships.
I guess if I had another relation let's say inside the Instructor class like "public InstructorType Type {get;set;}" I couldn't eager load that one?
@Nick-ACNB: Yes, you couldn't. But I think it is another limitation than the one mentioned by Tom Dykstra which is very special (one-to-one relationship and TPH inheritance (you have TPT)).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.