实体框架Eager从父实体加载派生实体和衍生实体的对象属性(TPT继承)

时间:2016-08-25 21:26:46

标签: c# entity-framework ef-code-first entity-framework-6 eager-loading

此问题解答了如何使用include:Eager loading property of derived class using Include

来急切加载派生类的属性

这个问题回答了如何从父实体加载派生实体的派生实体和对象属性,但我相信它依赖于延迟加载(如果我错了请纠正我):Entity Framework - Eager loading of subclass related objects

我的情况类似于这些,但是当我明确禁用延迟加载时,我希望从父实体中获取对派生实体的加载属性。以下是另一篇文章中的一个例子:

Person
{
   Name
   Address
}

Employee : Person
{
   Compensation - object
}

Visitor : Person
{

}

Company
{
    ICollection<Person> People
}

在这个例子中,我希望表达式在从Company集合查询时急切加载Compensation对象。我最初的想法是做这样的事情:

public IQueryable<Company> CompanyWithPeople() {
  return Context.Company
    .Include(c => c.People)
    .Include(c => c.People.OfType<Employee>().Select(e => e.Compensation));
}

但这会产生错误:

System.ArgumentException : The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

有没有办法从父实体中急切加载派生类的派生实体和对象属性?

0 个答案:

没有答案
相关问题