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.

4

1 回答 1

2

我不明白为什么...

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

......不应该工作。PersonsEF 应该用具体StudentInstructor实体填充列表。

您既没有“一对零或一关系”,也没有“派生类”上的导航属性(Training.Persons)。因此,我认为上述限制不适用于您的模型和查询。

于 2013-07-05T14:53:48.477 回答