Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF Core 3.0.Include()链条比2.2长约5-10倍_C#_Linq_Entity Framework Core - Fatal编程技术网

C# EF Core 3.0.Include()链条比2.2长约5-10倍

C# EF Core 3.0.Include()链条比2.2长约5-10倍,c#,linq,entity-framework-core,C#,Linq,Entity Framework Core,我遇到了一个大型EF核心查询的问题,该查询包含大量链接的.Include()。 我有一个linq查询,如下所示: \u context.device.Include(x=>x.Group) .包括(x=>x.Status) .包括(x=>x.面积) .包括(x=>x.EquipmentType) .Include(x=>x.Parts)。然后Include(x=>x.ChildrenParts) .Include(x=>x.Parts)。然后Include(x=>x.Parts) .Includ

我遇到了一个大型EF核心查询的问题,该查询包含大量链接的
.Include()
。 我有一个linq查询,如下所示:

\u context.device.Include(x=>x.Group)
.包括(x=>x.Status)
.包括(x=>x.面积)
.包括(x=>x.EquipmentType)
.Include(x=>x.Parts)。然后Include(x=>x.ChildrenParts)
.Include(x=>x.Parts)。然后Include(x=>x.Parts)
.Include(x=>x.Parts)。然后Include(x=>x.Vendor)
.包括(x=>x.Notes)
.包括(x=>x.部门)
.包括(x=>x.P维护)
.Include(x=>x.SystemInfo)。然后Include(x=>x.SystemUsers)
.Include(x=>x.SystemInfo)。然后Include(x=>x.Frameworks)
.Include(x=>x.SystemInfo)。然后Include(x=>x.VideoCards)
.Include(x=>x.SystemInfo)。然后Include(x=>x.StorageDrives)
.Include(x=>x.SystemInfo)。然后Include(x=>x.Software)
.Include(x=>x.SystemInfo)。然后Include(x=>x.NetworkAdapters)
.Include(x=>x.SystemInfo)。然后Include(x=>x.Printers)
.包括(x=>x.维护小时数)
.包括(x=>x.附件)
.Include(x=>x.Request)
.FirstOrDefault(x=>x.EquipmentId==id);
在EF Core 2.2中,这花费了不到几秒钟的时间进行评估。现在在EF Core 3.0上,大约需要15秒才能完成。那么EF Core 3又是如何让这一切变得如此缓慢的呢?我了解到ef现在为每个linq查询创建一条sql语句,但我不知道在这个实例中该语句会发生什么变化。我可以对这个查询做些什么来减少执行时间吗


编辑:这是在SQL Server v11.0.3上进行的

像这样尝试一下。您可能需要更改一些“选择”和“选择多个”选项以及Id字段名称,因为您没有发布上下文`

var query = _context.Equipment.Include(x => x.Group)
            .Include(x => x.Status)
            .Include(x => x.Area)
            .Include(x => x.EquipmentType)
            .Include(x => x.Notes)
            .Include(x => x.Department)
            .Include(x => x.PMaintenance)
            .Include(x => x.MaintenanceHours)
            .Include(x => x.Attachments)
            .Include(x => x.Request).FirstOrDefault(x => x.EquipmentId == id);

            query.Include(x => x.Parts).ThenInclude(x => x.ChildrenParts).SelectMany(x => x.Parts).Where(a => query.Select(q => q.PartsId).Contains(a.Id)).Load();
            query.SelectMany(x => x.Parts).SelectMany(x => x.ChildrenParts).Load();
            query.SelectMany(x => x.Parts).SelectMany(x => x.ParentParts).Load();
            query.SelectMany(x => x.Parts).Select(x => x.Vendor).Load();
            query.Include(x => x.SystemInfo).ThenInclude(x => x.SystemUsers).SelectMany(x => x.SystemInfo).Where(a => query.Select(q => q.SystemInfoId).Contains(a.Id)).Load();
            query.SelectMany(x => x.SystemInfo).SelectMany(x => x.Frameworks).Load();
            query.SelectMany(x => x.SystemInfo).SelectMany(x => x.VideoCards).Load();
            query.SelectMany(x => x.SystemInfo).SelectMany(x => x.StorageDrives).Load();
            query.SelectMany(x => x.SystemInfo).SelectMany(x => x.Software).Load();
            query.SelectMany(x => x.SystemInfo).SelectMany(x => x.NetworkAdapters).Load();
            query.SelectMany(x => x.SystemInfo).SelectMany(x => x.Printers).Load();

            query.ToList();

希望这有帮助

为什么不直接调用一个包含所有字段的视图呢?可能有必要检查不同EF版本之间生成的SQL查询,看看是否有任何更改。哇,有很多包含。每当你在做这样的事情时,你都需要问问自己是否有更好的方法。我可以缓存所有这些实体并在一段时间内更新它们,但问题是这在2.2上不是问题。@IvanStoev看起来已经有了这样的方法!