且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EF Lambda:包含路径表达式必须引用导航属性

更新时间:2023-09-29 08:10:46

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