title: Entity Framework Slow AddRange Insert to DB
body: <p>I am implementing a Database First Entity Framework 6.1 to insert about 2000 master/detail collections to SQL server db. Each collection has about two to three objects. So the the total number or records to be inserted is ~5000. The transaction takes about two to three minutes which is very slow. I am using the below code:</p>
  <pre class="lang-cs prettyprint-override"><code>    public class Collection
      {
          [Key]
          public int Id { get; set; }
          public string Name { get; set; }
  
          ...
  
          public List&lt;DetailCol&gt; Details{ get; set; } 
      }
  
  
      public class DetailCol
      {
              [Key]
              public int Id { get; set; }
              public decimal Lvl { get; set; }
              public string Type { get; set; }
      }
  
      var MyCollections = new List&lt;Collection&gt;();
      
      // Do population of collection and try to insert to db
  
      using (var db = new MyContext(ConnectionString)
      {
          // Speed up insert
          db.Configuration.AutoDetectChangesEnabled = false;
      
          // Add new entries to master and details
          db.MyCollections.AddRange(MyCollections);
      
          // Update db
          db.SaveChanges();
      }
  </code></pre>
  <p>Can someone give a hint why this is happening and how to improve that?</p>