我有類,如:預先加載屬性包括
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