【发布时间】:2019-11-03 03:08:36
【问题描述】:
我在 EF Core 2.0 中有这样的 linq 查询,它按原样工作,但是当我升级到 EF Core 3.0 时它总是超时。我在query = query.Where(x => x.Questions); 中发现了这个问题。
我的问题是我想返回带有过滤问题的课程,例如仅 Take(10) 或仅显示特定范围而不是所有问题的 .Where 条件。
var query = _courseRepository.Table;
query = query.Where(x => x.Id == id);
query = query.Include(x => x.Questions);
query = query.Include(x => x.CourseYear);
query = query.Include(x => x.CourseSubject);
query = query.Include(x => x.Instructors).ThenInclude(y => y.User);
query = query.Include(x => x.Instructors).ThenInclude(y => y.Course);
query = query.Include(x => x.Instructors).ThenInclude(y => y.CourseClass);
query = query.Include(x => x.CourseSections);
query = query.Include(x => x.CourseSections).ThenInclude(y => y.Lessons);
query = query.Include(x => x.CourseClasses);
query = query.Include(x => x.UserCourses).ThenInclude(y => y.User);
var result = query.FirstOrDefault();
【问题讨论】:
-
请使用 SQL Trace 来获取 2.2 正在执行的 SQL。然后对 3.0 执行相同的操作。请在您的问题中分享它们。
-
I found the issue in query = query.Where(x => x.Questions);.该代码不在您的问题中。 -
我想知道如何返回包含条件的问题,因为我尝试了几种方法它总是返回所有列表。
-
看起来您正在使用
.Include处理一对多或多对多关系。您不应该为此使用它 - 您应该只将.Include()用于零或一对一或多对一关系。 -
@Dai 来自 EF Docs 是允许这个。包括一对多,你的意思是你不应该使用它。
标签: c# ef-core-2.2 ef-core-3.0