使用Include来获取派生类的加载属性

Eri*_*c P 15 inheritance entity-framework ef-code-first

我有类似的课程:

Person
{
   Name
   Address
}

Employee : Person
{
   Compensation - object
}

Visitor : Person
{

}
Run Code Online (Sandbox Code Playgroud)

如果我写linq:

var persons = Context.Persons
                .Include("Compensation");
Run Code Online (Sandbox Code Playgroud)

我收到错误:

指定的包含路径无效.EntityType"Person"未声明名为"Compensation"的导航属性.

如果我这样做,它可以正常工作

var persons = Context.Persons
                .OfType<Employee>()
                .Include("Compensation");
Run Code Online (Sandbox Code Playgroud)

但我想让员工和访问者在同一个查询中.

看起来在EF4 UserVoice上有此功能的请求:http: //data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/1249289-include-property-of衍生类?REF =标题

但它看起来不会很快就会完成.

这个问题有什么好的解决方法?

Lad*_*nka 7

你可以这样试试:

var persons = Context.Persons
                     .OfType<Employee>()
                     .Include("Compensation")
                     .Concat<Person>(Context.Persons.OfType<Visitor>());
Run Code Online (Sandbox Code Playgroud)