Entity framework EF 6.Include()优化

Entity framework EF 6.Include()优化,entity-framework,entity-framework-6,Entity Framework,Entity Framework 6,我有一个大对象,它需要在任何时候加载它的所有成员 rtrn.List = Context.Days .Include(x => x.Inspections.Select(y => y.Type)) .Include(x => x.Inspections.Select(y => y.Subtype)) .Include(x => x.Inspections.Select(y => y.Inspector

我有一个大对象,它需要在任何时候加载它的所有成员

rtrn.List = Context.Days
          .Include(x => x.Inspections.Select(y => y.Type))
          .Include(x => x.Inspections.Select(y => y.Subtype))
          .Include(x => x.Inspections.Select(y => y.Inspector))
          .Include(x => x.Inspections.Select(y => y.Approver))
          .Include(x => x.Inspections.Select(y => y.OperatorAddress))
          .Include(x => x.Inspections.Select(y => y.Unit))
          .Include(x => x.Inspections.Select(y => y.RecordsAddress))
          .Include(x => x.Inspections.Select(y => y.InspectionAddress))
          .Include(x => x.Inspections.Select(y => y.People))
          .Include(x => x.Inspections.Select(y => y.CustomFields))
          .Include(x => x.Inspections.Select(y => y.Codelinks.Select(s => s.CodeSections.Select(q => q.Questions.Select(a => a.Answer)))))
          .Include(x => x.Inspections.Select(y => y.Attachments))
          .Include(x => x.Inspections.Select(y => y.Comments))
        .Include(x => x.Inspections.Select(y => y.ComplianceRecords.Select(z => z.Records)))
          .Include(x => x.Inspections.Select(y => y.CorrespondenceRecords))
          .Where(x => x.Inspector.Id == userId).OrderByDescending(x => x.DayDate).Skip(page * perpage).Take(perpage).ToList();
这需要很长的时间来加载,我正在寻找如何加快加载速度的想法。 通过我的研究,我看到了使用.Load方法代替includes的建议,所以我尝试了一下

var rtrn = Context.Days..Where(x => x.Inspector.Id == userId).OrderByDescending(x => x.DayDate).Skip(page * perpage).Take(perpage).ToList();

foreach (Day d in rtrn)
        {
            Context.Entry(d).Collection(x => x.Inspections).Load();
            foreach (Inspection insp in d.Inspections)
            {
                Context.Entry(insp).Reference(x => x.Type).Load();
                Context.Entry(insp).Reference(x => x.Subtype).Load();
                Context.Entry(insp).Reference(x => x.Inspector).Load();
                Context.Entry(insp).Reference(x => x.Approver).Load();
                Context.Entry(insp).Reference(x => x.OperatorAddress).Load();
                Context.Entry(insp).Reference(x => x.Unit).Load();
                Context.Entry(insp).Reference(x => x.RecordsAddress).Load();
                Context.Entry(insp).Reference(x => x.InspectionAddress).Load();
                Context.Entry(insp).Collection(x => x.People).Load();
                Context.Entry(insp).Collection(x => x.CustomFields).Load();
                Context.Entry(insp).Collection(x => x.Codelinks).Load();
                Context.Entry(insp).Collection(x => x.Attachments).Load();
                Context.Entry(insp).Collection(x => x.Comments).Load();
                Context.Entry(insp).Collection(x => x.ComplianceRecords).Load();
                Context.Entry(insp).Collection(x => x.CorrespondenceRecords).Load();

                foreach (CodeLink cl in insp.Codelinks)
                {
                    Context.Entry(cl).Collection(x => x.CodeSections).Load();
                    foreach (CodeSection cs in cl.CodeSections)
                    {
                        Context.Entry(cs).Collection(x => x.Questions).Load();
                        foreach (Question q in cs.Questions)
                            Context.Entry(q).Reference(x => x.Answer).Load();
                    }
                }

                foreach (ComplianceRecordBucket b in insp.ComplianceRecords)
                    Context.Entry(b).Collection(x => x.Records).Load();
            }
        }

根据我的测量,这需要大约10倍的时间。不能说我很惊讶,但这让我不知道该从何处解决这个问题。

为什么不从模型中删除延迟加载属性?你是说在public virtual EF.User.User Approver{get;set;}中的virtual as?如果有的话,就没有了。因此,我需要始终使用.Include.large object(需要加载其所有成员)。这是一句话中的三个糟糕的设计决策。不要试图通过运行单独的查询来修复它。咬紧牙关,改进设计。这不是游戏。“但我认为格特…”格塔诺并不反对。但是,这个查询会累积到一个API,客户端应用程序需要整个对象,这就是它的本质。需要与现实情况合作。