使用Include加载派生类的属性 [英] Eager loading property of derived class using Include

查看:145
本文介绍了使用Include加载派生类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类:

Person
{
   Name
   Address
}

Employee : Person
{
   Compensation - object
}

Visitor : Person
{

}

如果我写linq:

var persons = Context.Persons
                .Include("Compensation");

我收到错误:


指定的包含路径无效。
EntityType'Person'不
声明一个导航属性,
名称为'薪酬'。

A specified Include path is not valid. The EntityType 'Person' does not declare a navigation property with the name 'Compensation'.

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

It works ok if I do:

var persons = Context.Persons
                .OfType<Employee>()
                .Include("Compensation");

但是,我希望让同事和员工获得相同的查询。

But I would like to get Employees and visitors in the same query.

看起来EF4 UserVoice有此功能的请求:
http://data.uservoice.com/forums/72025-ado-net- entity-framework-ef-feature-suggestions / suggestions / 1249289-include-property-of-derived-classes?ref = title

Looks like there is a request for this feature on EF4 UserVoice: http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/1249289-include-property-of-derived-classes?ref=title

但它不

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

What is a good workaround for this issue?

推荐答案

您可以这样尝试:

var persons = Context.Persons
                     .OfType<Employee>()
                     .Include("Compensation")
                     .Concat<Person>(Context.Persons.OfType<Visitor>());

这篇关于使用Include加载派生类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆