EF Lambda:包含路径表达式必须引用导航属性 [英] EF Lambda: The Include path expression must refer to a navigation property
问题描述
这是我的表达:
Course course = db.Courses
.Include(
i => i.Modules.Where(m => m.IsDeleted == false)
.Select(s => s.Chapters.Where(c => c.IsDeleted == false))
).Include(i => i.Lab).Single(x => x.Id == id);
我知道原因是 Where(m => m.IsDeleted = = false)
,但为什么会导致错误?更重要的是,如何解决?
I know the cause is Where(m => m.IsDeleted == false)
in the Modules portion, but why does it cause the error? More importantly, how do I fix it?
如果我删除where子句,它可以正常工作,但我想过滤出删除的模块。
If I remove the where clause it works fine but I want to filter out deleted modules.
推荐答案
.Include
用于从数据库中加载相关实体。即在您的情况下,请确保模块和实验室的数据已加载课程。
.Include
is used to eagerly load related entities from the db. I.e. in your case make sure the data for modules and labs is loaded with the course.
.Include
中的lamba表达式应该告诉Entity Framework要包括的相关表。
The lamba expression inside the .Include
should be telling Entity Framework which related table to include.
在你的情况下,你也试图在include中执行一个条件,这就是为什么你收到一个错误。
In your case you are also trying to perform a condition inside of the include, which is why you are receiving an error.
看起来你的查询是这样的:
It looks like your query is this:
找到匹配的课程给定id,与相关模块和实验室。只要匹配的模块和章节不被删除。
Find the course matching a given id, with the related module and lab. As long as the matching module and chapter are not deleted.
如果是正确的,那么这应该是有效的:
If that is right, then this should work:
Course course = db.Courses.Include(c => c.Modules)
.Include(c => c.Lab)
.Single(c => c.Id == id &&
!c.Module.IsDeleted &&
!c.Chapter.IsDeleted);
这篇关于EF Lambda:包含路径表达式必须引用导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!