title: Entity Framework bulk inserts - creating a large number of objects / entities is the slow part?
body: <p>There's a lot of information and answers on here and elsewhere about how to speed up bulk inserts with EF.</p>
  
  <p>However, I'm only inserting about 10,000 records and the slow part of creating the entities.</p>
  
  <p>First, I save the external data to a datatable, then loop through and for each row create a new <code>entity</code>, add the child entities in the loop (sourced from a couple of other datatables) and once the loop is finished call <code>db.SaveChanges()</code>.</p>
  
  <p>In my initial profiling, the <code>db.SaveChanges()</code> is slow, but nothing compared to the loop of creating all the objects in the first place - either as a separate <code>List&lt;entity&gt; entities</code> or directly to the context.</p>
  
  <p>Is this usual? As of all the issues with bulk insert I can see, most seem to be related to the final commit to the database.</p>
  
  <hr>
  
  <p><em>Edit to add some code</em> Please excuse the psudo-code</p>
  
  <pre><code>DataTable ref1 = ConvertCSVtoDataTable(csv, firstRowsToDelete: 15); // Return's a Datatable from a CSV
  
  foreach(string file in ListOfFilesToProcess)
  {
      DataTable tbl = loadExcelFiles(file);
  
      foreach(DataRow dr in tbl.Rows)
      {
           Entity newEntity = new Entity();
           Entity.property1 = dr["Property1"].ToString();
           ... // Keep mapping properties to elements in the datatable
           Entity.Child.Add(new ChildEntity() { prop1 = ref1["ChildProp1"].ToString() });
           // Add the newly created entity to the context
           db.Entity.Add(newEntity);
      }
  }
  // Save the context
  db.SaveChanges();
  </code></pre>
  
  <p>So by the point of saving the context there's a few thousand <code>newEntity</code> objects and their child <code>navigation</code> objects.</p>
  
  <p>Iterating over the datatable and creating all these objects is the part which is the slowest!</p>
  
  <p>Also, <code>db.Configuration.AutoDetectChangesEnabled = false;</code> has already been set.</p>