Eager loading property of derived class using Include

前端 未结 4 1552
我在风中等你
我在风中等你 2020-12-09 10:41

I have classes like:

Person
{
   Name
   Address
}

Employee : Person
{
   Compensation - object
}

Visitor : Person
{

}

If I write linq:<

4条回答
  •  醉话见心
    2020-12-09 11:20

    Here's a nice example of how to load Persons and include Compensation for Employees

    Replace Reference() by Collection() for a collection property.

    IQueryable GetPersons()
    {
        var persons = Context.Persons;
        foreach(var entry in persons.OfType())
            Context.Entry(entry).Reference(e => e.Compensation).Load();
        return persons;
    }
    

    Not sure either if it's efficient, but it works, and the intention is clearer than with joins.

    Based on this SO answer

提交回复
热议问题