سؤال

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.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top