我有这样的类(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/