Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
如何减少实体框架创建的SQL数量?_Sql_Entity Framework - Fatal编程技术网

如何减少实体框架创建的SQL数量?

如何减少实体框架创建的SQL数量?,sql,entity-framework,Sql,Entity Framework,我有下面一段Linq要引用 private IQueryable<VRTSystem> GetObjectSetWithIncludes() { return GetObjectSet() .Include(s => s.Site.Address.GeographicalLocation.ParentLocation.ParentLocation) .Include(s => s.Dhr.Select(d =

我有下面一段Linq要引用

    private IQueryable<VRTSystem> GetObjectSetWithIncludes() {
        return GetObjectSet()
          .Include(s => s.Site.Address.GeographicalLocation.ParentLocation.ParentLocation)
          .Include(s => s.Dhr.Select(d => d.DhrParts.Select(dp => dp.PhysicalPart.PartDefinition)))
          .Include(s => s.Room.RoomType)
          .Include(s => s.ProductConfiguration.ProductConfigurations_PriceBook.Select(pcpb => pcpb.PriceBook))
          .Include(s => s.ProductConfiguration.Quote_ProductConfigurations)
          .Include(s => s.ProductConfigurationTemplate)
          .Include(s => s.Customer.GeographicalLocation.ParentLocation.ParentLocation)
          .Include(s => s.Customer.CustomerType)
          .Include(s => s.HospitalGroupCustomer.GeographicalLocation.ParentLocation.ParentLocation)
          .Include(s => s.HospitalGroupCustomer.CustomerType)
          .Include(s => s.ServiceCoverHistories.Select(sch => sch.ServiceCoverLevel))
          .Include(s => s.SystemStatus)
          .Include(s => s.ShippingContact.Address)
          .Include(s => s.Quote)
          .Include(s => s.PaidDate)
          .Include(s => s.VRTSystemProductConfigurations
            .Select(
              spc =>
                spc.ProductConfigurationTemplate.ProductConfigurations_PriceBook.Select(
                  pcpb => pcpb.PriceBook)))
          .Include(s => s.VRTSystemProductConfigurations
            .Select(spc => spc.PaidDate))
          .Include(s => s.Quote_ProductConfigurations
            .Select(spc => spc.ProductConfiguration))
          .Include(s => s.ProductConfigurationTemplate
            .ProductConfigurationOptions.Select(pco => pco.ProductConfigurationOptionValues))
          .Include(s => s.SystemProductConfigurationOptionsSelectedValues)
          .Include(s => s.VRTSystemProductConfigurations
            .Select(spc => spc.UpgradeProductConfigurationOptionsSelectedValues));
    }
}
延迟加载被关闭,因为我们正在通过WCF发送结果,因此需要立即加载所有数据。此外,将使用数据的WPF窗口需要所有这些相关表

这已经运行了很长时间,但突然开始抛出一个异常,即查询中的表太多。我查看了生成的SQL,它几乎有20000行,完成了600多个连接!我并不奇怪它抛出了如此多的SQL异常

我真的不知道为什么它会有这么多的连接,因为查询没有引用接近这个数量的表


你知道为什么会突然抛出这个异常吗?我能做些什么来改进这个查询?我意识到最好的答案可能是做一些重大的改写,把它拆分成更小的查询,但是除了这样做的复杂性之外,我需要一个快速的解决方案,因为部署的系统不再工作,我们需要让它再次紧急运行。对部分数据使用即时加载,并单独使用显式加载调用,以较小的查询填充对象图的其余部分。要准确地确定如何分解它,需要了解数据模型,可能还需要一些尝试和错误。我还需要重新考虑是否需要从一个WCF调用返回整个对象图,或者是否有部分数据可以通过单独的调用按需加载。是的,我已经想到了这一点,但问题是整个对象图会按原样发送到客户端,并且绑定到一个相当复杂的WPF窗口。分解它需要大量的黑客操作才能将较小的部分粘在一起。我担心的是为什么会突然发生这种情况。查询很长一段时间以来一直运行良好。今天,在一段完全独立的代码中出现了同样的问题,该代码的查询要简单得多。我不明白为什么它突然生成SQL抛出异常,而它已经正常工作了这么长时间。嗯,同样的问题刚刚出现在代码的另一个区域,这次是一个非常简单的Linq查询。如果我必须开始分解哪怕是简单的Linq查询,那么我也不必首先考虑实体框架。这里发生了一些奇怪的事情。我们没有更改代码,但以前有效的查询现在不起作用了。我很想知道为什么。