简体   繁体   English

创建从EF 4.0到SQL Server 2008 R2的单个查询,而不是多个查询

[英]Create a single Query from EF 4.0 to SQL Server 2008 R2 instead of multiple queries

In following code, multiple queries will be run against the database for inserting one row at a time. 在下面的代码中,将对数据库运行多个查询以一次插入一行。

EF cause Multiple Database Trips: When I call c.SaveChanges, then EF 4.0 will run multiple UPDATE statements against the database, but will execute only one UPDATE per database trip. EF导致多个数据库行程当我调用c.SaveChanges时,EF 4.0将对数据库运行多个UPDATE语句,但每个数据库行程仅执行一个UPDATE。 This means if we have 1000 UPDATE statements generated by EF 4.0, then we will end up making 1000 database trips, which is NOT a GOOD and OPTIMAL data approach in my view. 这意味着,如果我们有EF 4.0生成的1000条UPDATE语句,那么我们最终将进行1000次数据库行程,这在我看来不是一种GOOD和OPTIMAL数据方法。

Is there any way of making EF 4.0 run a single batched query for all records updates in a single database trip? 有什么方法可以使EF 4.0对单个数据库行程中的所有记录更新运行单个批处理查询? May be, if I could get the UPDATE query ( I mean the SQL query) for each record update, then I could just put all these queries into a single batched query and send the batched query to database, but I don't know if the UPDATE query for each record can be obtained in EF 4.0. 可能是,如果我可以获取每个记录更新的UPDATE查询(我的意思是SQL查询),那么我可以将所有这些查询放入单个批处理查询中并将该批处理查询发送到数据库,但是我不知道是否可以在EF 4.0中获得每个记录的UPDATE查询。

 public void SaveVendorOrders(int vendorId, int parentVendorId, string tag,
                              string desc, int productSegment, int productCategory)     
 {  
  using (Vendor c = new Vendor())
        {
            var query = (from p in c.Orders
                         where (p.VendorID == vendorId ||
                                p.ParentVendorID == parentVendorId)
                         select p);

            if (query.ToList().Count() > 0)
            {
                for (int i = 0; i <= query.ToList().Count() - 1; i++)
                {
                    query.ToList()[i].OrderTag = tag;
                    query.ToList()[i].OrderDescription = desc;
                    query.ToList()[i].ProductSegment = productSegment;
                    query.ToList()[i].ProductSegmentCategory = productCategory;
                }

                c.SaveChanges();
            }
        }
 }

EDIT 1: 编辑1:

As pointed out by SJuan76 and Hamlet Hakobyan, calling ToList repeatedly will execute a query repeatedly. 正如SJuan76和Hamlet Hakobyan指出的那样,重复调用ToList将重复执行查询。 But even that is solved by cleaning the code so ToList is called only once, my question remains: How to combine multiple updates into a single batched update query? 但是即使通过清理代码就解决了问题,因此ToList仅被调用一次,我的问题仍然存在:如何将多个更新合并到一个批处理更新查询中?

The query intended to deferred execution. 该查询旨在推迟执行。 It really executes whey you enumerate your query or, for instance, call ToList() . 它确实执行了您枚举查询的乳清,或者,例如,调用ToList() To avoid multiple enumerations you must call ToList() one time the your in-memory data in you method. 为了避免多个枚举,必须在方法中一次调用ToList()

using (Vendor c = new Vendor())
{
  var query = (from p in c.Orders
               where (p.VendorID == vendorId ||
                      p.ParentVendorID == parentVendorId)
               select p).ToList();

  if (query.Count() > 0)
  {
      for (int i = 0; i <= query.Count() - 1; i++)
      {
          query[i].OrderTag = tag;
          query[i].OrderDescription = desc;
          query[i].ProductSegment = productSegment;
          query[i].ProductSegmentCategory = productCategory;
      }

      c.SaveChanges();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM