简体   繁体   English

实体框架Include():指定的包含路径无效

[英]Entity Framework Include() : A specified Include path is not valid

I have the following Entity Framework statement that has been working correctly. 我有以下已正常运行的Entity Framework语句。

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

However, I needed to disable lazy loading for this code and so I've added an Include() element to the previous statement: 但是,我需要禁用此代码的延迟加载,因此在前面的语句中添加了Include()元素:

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

However, this generates a run-time exception: 但是,这会生成运行时异常:

A specified Include path is not valid. 指定的包含路径无效。 The EntityType 'Leo.Domain.CostingEvent' does not declare a navigation property with the name 'Costing'. EntityType'Leo.Domain.CostingEvent'未声明名称为'Costing'的导航属性。

I really don't understand this error. 我真的不明白这个错误。 First of all, I am not referencing CostingEvent.Costing , I'm referencing CostingEvent.ProposalSection.Proposal.Costing . 首先,我没有引用CostingEvent.Costing ,而是引用CostingEvent.ProposalSection.Proposal.Costing Furthermore, these are all valid navigation properties that show up in Intellisense. 此外,这些都是在Intellisense中显示的有效导航属性。

Note: This is a database-first application. 注意:这是一个数据库优先的应用程序。 Also note: repository is a wrapper class but the Include() reference is standard Entity Framework. 还要注意: repository是包装器类,但是Include()引用是标准的Entity Framework。

Nested EF Includes are tricky. 嵌套的EF Includes非常棘手。

Have you considered 你有没有考虑过

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

This may also work 这可能也可以

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);

You need to select the subnavigation properties like this: 您需要选择子导航属性,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM