title: Big Batch of Entity Framework Updates Much Slower Than Batching Myself
body: <p>Updating a bunch of records is much slower using what I think are standard entity framework techniques than batching the same queries it would generate myself.  For 250 records I see entity framework about 10 times as slow.  For 1000 records it goes up to about 20 times slower.</p>
  
  <p>When I log the database activity for entity framework, I see it is generating the same basic queries I would generate myself, but it seems to be running them one at a time instead of all at once, even though I only call SaveChanges once.  Is there any way to ask it to run the queries all at once?</p>
  
  <p>I can't do a simple mass SQL update because in my real use case each row needs to be processed separately to determine what to set the fields to.</p>
  
  <p>Sample timing code is below:</p>
  
  <pre><code>var stopwatchEntity = new System.Diagnostics.Stopwatch();
  var stopwatchUpdate = new System.Diagnostics.Stopwatch();
  using (var dbo = new ProjDb.dbo("Server=server;Database=database;Trusted_Connection=True;"))
  {
      var resourceIds = dbo.Resources.Select(r =&gt; r.ResourceId).Take(250).ToList();
  
      //dbo.Database.Log += (s) =&gt; System.Diagnostics.Debug.WriteLine(s);
  
      stopwatchEntity.Start();
      foreach (var resourceId in resourceIds)
      {
          var resource = new ProjDb.Models.dbo.Resource { ResourceId = resourceId };
          dbo.Resources.Attach(resource);
          resource.IsBlank = false;
      }
      dbo.SaveChanges();
      stopwatchEntity.Stop();
  
      stopwatchUpdate.Start();
      var updateStr = "";
      foreach (var resourceId in resourceIds)
          updateStr += "UPDATE Resources SET IsBlank = 0 WHERE ResourceId = " + resourceId + ";";
      dbo.Database.ExecuteSqlCommand(updateStr);
      stopwatchUpdate.Stop();
  
      MessageBox.Show(stopwatchEntity.Elapsed.TotalSeconds.ToString("f") + ", " + stopwatchUpdate.Elapsed.TotalSeconds.ToString("f"));
  }
  </code></pre>