entity-framework - 使用 Include 的派生类的急切加载属性

标签 entity-framework inheritance ef-code-first

我有这样的类(class):

Person
{
   Name
   Address
}

Employee : Person
{
   Compensation - object
}

Visitor : Person
{

}

如果我写 linq:
var persons = Context.Persons
                .Include("Compensation");

我得到错误:

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



如果我这样做,它可以正常工作:
var persons = Context.Persons
                .OfType<Employee>()
                .Include("Compensation");

但我想在同一个查询中获取员工和访客。

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

但它看起来不会很快完成。

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

最佳答案

你可以这样试试:

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

关于entity-framework - 使用 Include 的派生类的急切加载属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6263288/

相关文章:

c# - 尝试 catch block 还是 bool 标志?

sql - 配置文件 Entity Framework

java - 使用继承概念访问实例变量

javascript - 继承类的对象可以访问父类的所有属性和方法

c# - 将 EF Code First 应用程序部署到生产数据库

entity-framework - 当实体具有与外键的交叉引用时的代码优先迁移

entity-framework - Azure 数据资源管理器 C# 查询和 ORM

java - 子类是否继承父类(super class)的私有(private)实例变量

entity-framework - Entity Framework 双向关系未加载包括

c# - EF6 - 空间/全文/哈希索引和显式索引顺序的使用不正确