2015-10-05 132 views
0

我已經正常工作下實體框架的語句。實體框架包括():指定包含路徑無效

CostingEvent targetEvent = repository.Query<CostingEvent>() 
            .FirstOrDefault(ce => ce.Id == targetId); 

不過,我需要禁用延遲加載此代碼,所以我增加了一個Include()元素前面的語句:

CostingEvent targetEvent = repository.Query<CostingEvent>() 
            .Include(ce => ce.ProposalSection.Proposal.Costing) 
            .FirstOrDefault(ce => ce.Id == targetId); 

然而,這會產生一個運行時異常:

一個指定的包含路徑無效。 EntityType'Leo.Domain.CostingEvent'不聲明名爲'Costing'的導航屬性。

我真的不明白這個錯誤。首先,我不參考CostingEvent.Costing,我參考CostingEvent.ProposalSection.Proposal.Costing。此外,這些都是在Intellisense中顯示的所有有效導航屬性。

注:這是一個數據庫的首次應用。另外請注意:repository是一個包裝類,但Include()參考標準是實體框架。

+0

我認爲所有的屬性映射? –

+0

你能否提供相應課程的要點?這應該照原樣工作。 –

回答

0

嵌套EF包括有貓膩。

你有沒有考慮

CostingEvent targetEvent = repository.Query<CostingEvent>() 
           .Include("ProposalSection.Proposal.Costing") 
           .FirstOrDefault(ce => ce.Id == targetId); 

這也可能工作

CostingEvent targetEvent = repository.Query<CostingEvent>() 
           .Include(ce => ce.ProposalSection) 
           .Include(ce => ce.ProposalSection.Proposal) 
           .Include(ce => ce.ProposalSection.Proposal.Costing) 
           .FirstOrDefault(ce => ce.Id == targetId); 
+0

我看到支持的語法,但我不明白爲什麼它會不同。 –

+0

這並沒有什麼不同,第三個包含是你所需要的(這意味着前兩個)。 –

0

您需要選擇子導航性能是這樣的:

CostingEvent targetEvent = repository.Query<CostingEvent>() 
            .Include(ce => ce.ProposalSection.Select(ps=>ps.Proposal.Select(p=>p.Costing))) 
            .FirstOrDefault(ce => ce.Id == targetId); 
+0

不是'Select()'需要點左側的集合嗎? 「ProposalSection」和「Proposal」都是單數。 –

+0

是的,選擇通常需要左側的集合。成本是導航屬性還是僅僅是提案的屬性?如果成本計算只是一個常規屬性,那麼您不需要擁有它,只需以提案結束即可。 –