我有类,如:预先加载属性包括
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
,但它并不像它会被很快完成任何时间。
这个问题的解决方法是什么?
变化的concat行:.Concat(Context.Persons.OfType ());否则不编译。运行时仍然会得到与原始问题相同的错误。 –
2011-06-07 10:14:13
@Ladislav,在我的场景中,Person类型不是抽象的,有的实体只是'Person'既不''Employee'或'Visitor'。你的例子不会给我那些**人的**。试图结合'Person'查询导致OP报告的异常。任何解决方法将非常感激(在我的情况下,它不是'Person',它是'Transaction',但不管)。 – Shimmy 2012-02-28 09:14:22
@Shimmy:以问题为例提出一个单独的问题,并引用这个问题。评论不太适合解决另一个(甚至类似的)问题。 – 2012-02-28 09:22:32