title: C# .NET 4.6.1 Entity Framework - DB.MyTable.Add(...) is slow despite NOT calling DB.SaveChanges()
body: <p><strong>(A)</strong> This version is <strong>slow</strong>... duration is measured in multiple minutes</p>
  <ul>
  <li>DB is a typical EF Data Context to a SQL Serve database</li>
  <li><code>AA_Words_100</code> is a simple SQL Server table which is added to the EF designer</li>
  <li><code>DB.AA_Words_100.Add</code> is called ~3,000 times  (confirmed via debugging with counter variables)</li>
  <li>I have confirmed that &gt;99% of the runtime is inside the inner loop</li>
  <li><code>XCampaign</code> is a <code>Dictionary&lt;string, Dictionary&lt;string, _Word&gt;&gt;</code> where _Word is a trivial non-EF object.</li>
  </ul>
  <pre><code>foreach (var XCampaign in Words100)
      foreach (var KVP in XCampaign.Value)
          DB.AA_Words_100.Add(KVP.Value.To_AA_Word_100());
  
  DB.SaveChanges();
  </code></pre>
  <p><strong>(B)</strong> This version is <strong>fast</strong>... - <code>.Add()</code> is simply commented out to narrow the scope</p>
  <pre><code>var iTemp = 0;
  
  foreach (var XCampaign in Words100)
    foreach (var KVP in XCampaign.Value)
      iTemp++;
  
  DB.SaveChanges();
  </code></pre>
  <p><strong>(C)</strong> This version is <strong>fast</strong>. I simply fill up a List before calling <code>DB.AddRange(...)</code></p>
  <pre><code>var LIST_WordsToAdd = new List&lt;AA_Words_100&gt;();
  
  foreach (var XCampaign in Words100)
  {
      foreach (var KVP in XCampaign.Value)
      {
          LIST_WordsToAdd.Add(KVP.Value.To_AA_Word_100());
      }
  
      DB.AA_Words_100.AddRange(LIST_WordsToAdd);
  }
  
  DB.SaveChanges();
  </code></pre>
  <p><strong>(D)</strong> Documentation</p>
  <p>According to <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.add?view=efcore-3.1" rel="nofollow noreferrer"><code>DbContext.Add</code> documentation</a></p>
  <blockquote>
  <p>Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.</p>
  </blockquote>
  <p>In particular, when <code>SaveChanges()</code> is called.</p>
  <p>I recently migrated to EF from Linq-to-SQL in this application. Linq-to-SQL did not have this problem.</p>
  <p>What reason could there be for the <code>DB.AA_Words_100.Add(...)</code> command being so slow?</p>
  <p>Thank you!</p>
  <p>#Update - To_AA_Word_11() Code</p>
  <pre><code>public AA_Words_100 To_AA_Word_100()
  {
      var W = new AA_Words_100();
      W.Word = Word;
      W.Word_Clean = Word.Replace(&quot;'&quot;, &quot;&quot;);
      W.PhraseCount = PhraseCount;
      W.Clicks = Clicks;
      W.Impressions = Impressions;
      W.SalesAmt = SalesAmt;
      W.SalesOrders = SalesOrders;
      W.SalesUnits = SalesUnits;
      W.Spend = Spend;
      W.Campaign = Campaign;
      return W;
  }
  
          
  </code></pre>