无法使用Include或ThenInclude或Select / Many进行一次查询来加载相关数据 [英] Can not load related data with Include or ThenInclude or Select/Many with ONE query

查看:204
本文介绍了无法使用Include或ThenInclude或Select / Many进行一次查询来加载相关数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1个测试分配了1个TestType。

1 Test has 1 TestType assigned.

我需要将所有与Test或不与TestClass,Schoolclass,Subject和Assigned Pu学生关联的TestType实体加载( 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和的不同组合然后包括但没有机会使用PupilsTests进行测试。

I tried different combinations of Include and ThenInclude but no chance to get the Tests WITH the PupilsTests.

当我使用上下文开始查询时,我只能通过测试获得PupilsTests。 .tests

,但是这样的结果是,我只获得分配给Test
的TestTypes- innerjoin -但是我需要所有TestType及其测试和PupilsTest,并且希望在一个查询中使用它。

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


    }


推荐答案

EF6的EF Core等效语法

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问题现在在EF Core文档的包括多个级别 ion:

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进行一次查询来加载相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆