2015-10-05 84 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

是的,选择通常需要左侧的集合。成本是导航属性还是仅仅是提案的属性?如果成本计算只是一个常规属性,那么您不需要拥有它,只需以提案结束即可。 –