无法使用 Include 或 ThenInclude 或 Select/Many with ONE 查询加载相关数据 [英] Can not load related data with Include or ThenInclude or Select/Many with ONE query
问题描述
1 个测试分配了 1 个测试类型.
1 Test has 1 TestType assigned.
我需要加载与 Test 相关的所有 TestType 实体或 not 和 Schoolclass 、Subject 和指定的学生到测试 (PupilsTests)
I need to load all TestType entities with relation to a Test OR not and Schoolclass , Subject and Assigned Pupils to a test (PupilsTests)
.SelectMany 或 .Select 在运行时在这里失败.
The .SelectMany or .Select fails here during runtime.
我尝试了 Include 和 ThenInclude 的不同组合,但没有机会通过 PupilsTests 获得测试.
I tried different combinations of Include and ThenInclude but no chance to get the Tests WITH the PupilsTests.
当我使用 context.Tests
但这导致我只得到分配给测试的测试类型- innerjoin - 但我需要所有的测试类型和他们的测试和他们的学生测试,我希望它在一个查询中.
but that has the consequence that I get only the TestTypes assigned to a Test - innerjoin - but I need all TestTypes and their Tests and their PupilsTests and I want it in ONE query.
var testtypes = await context.TestTypes
.Include(x => x.Schoolclass)
.Include(x => x.Subject)
.Include(x => x.Tests.SelectMany(z => z.PupilsTests))
.Where(t => t.SchoolyearId == schoolyearId)
.AsNotTracking()
.ToListAsync();
public class TestType
{
public TestType()
{
Tests = new HashSet<Test>();
}
// Props removed for clarity
public int Id { get; set; }
public ISet<Test> Tests { get; set; }
public Schoolyear Schoolyear { get; set; }
public Schoolclass Schoolclass { get; set; }
public Subject Subject { get; set; }
public int SchoolyearId { get; set; }
}
public class Test
{
public Test()
{
PupilsTests = new HashSet<PupilTest>();
}
// Props removed for clarity
public int Id { get; set; }
public ISet<PupilTest> PupilsTests { get; set; }
public TestType TestType { get; set; }
}
推荐答案
EF Core 等效于 EF6 的语法
The EF Core equivalent syntax of EF6
.Include(x => x.Tests.SelectMany(z => z.PupilsTests))
是
.Include(x => x.Tests).ThenInclude(x => x.PupilsTests)
请注意,ThenInclude
中似乎存在 VS Intellisense 问题,因此只需键入以上内容,它就会成功编译并运行.
Note that there seems to be a VS Intellisense issue inside the ThenInclude
, so just type the above and it will compile successfully and work.
另请注意,EF Core 处理集合包含的内容不同(不像在 EF6 中那样使用单个查询),因此上面会生成并执行 3 个 SQL 查询.
Also please note that EF Core processes collection includes differently (does not use single query as in EF6), so the above would generate and execute 3 SQL queries.
更新: VS Intellisense 问题现在在 包括多个级别部分:
Update: The VS Intellisense issue now is specifically mentioned in the EF Core documentation under Including multiple levels section:
注意!
当前版本的 Visual Studio 提供不正确的代码完成选项,并且在集合导航属性之后使用 ThenInclude
方法时,可能会导致正确的表达式被标记为语法错误.这是在 https://github.com/dotnet/roslyn/上跟踪的 IntelliSense 错误的症状问题/8237.只要代码正确并且可以成功编译,就可以安全地忽略这些虚假的语法错误.
Current versions of Visual Studio offer incorrect code completion options and can cause correct expressions to be flagged with syntax errors when using the ThenInclude
method after a collection navigation property. This is a symptom of an IntelliSense bug tracked at https://github.com/dotnet/roslyn/issues/8237. It is safe to ignore these spurious syntax errors as long as the code is correct and can be compiled successfully.
这篇关于无法使用 Include 或 ThenInclude 或 Select/Many with ONE 查询加载相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!