title: Bulk inserts taking longer than expected using Dapper
body: <p>After reading <a href="https://www.gamasutra.com/view/news/170502/Indepth_SQL_Server__High_performance_inserts.php" rel="noreferrer">this article</a> I decided to take a closer look at the way I was using Dapper.</p>
  
  <p>I ran this code on an empty database</p>
  
  <pre><code>var members = new List&lt;Member&gt;();
  for (int i = 0; i &lt; 50000; i++)
  {
      members.Add(new Member()
      {
          Username = i.toString(),
          IsActive = true
      });
  }
  
  using (var scope = new TransactionScope())
  {
      connection.Execute(@"
  insert Member(Username, IsActive)
  values(@Username, @IsActive)", members);
  
      scope.Complete();
  }
  </code></pre>
  
  <p>it took about 20 seconds. That's 2500 inserts/second. Not bad, but not great either considering the blog was achieving 45k inserts/second. Is there a more efficient way to do this in Dapper?</p>
  
  <p>Also, as a side note, running this code through the Visual Studio debugger took <strong>over 3 minutes!</strong> I figured the debugger would slow it down a little, but I was really surprised to see that much.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>So this</p>
  
  <pre><code>using (var scope = new TransactionScope())
  {
      connection.Execute(@"
  insert Member(Username, IsActive)
  values(@Username, @IsActive)", members);
  
      scope.Complete();
  }
  </code></pre>
  
  <p>and this</p>
  
  <pre><code>    connection.Execute(@"
  insert Member(Username, IsActive)
  values(@Username, @IsActive)", members);
  </code></pre>
  
  <p>both took 20 seconds.</p>
  
  <p>But this took 4 seconds!</p>
  
  <pre><code>SqlTransaction trans = connection.BeginTransaction();
  
  connection.Execute(@"
  insert Member(Username, IsActive)
  values(@Username, @IsActive)", members, transaction: trans);
  
  trans.Commit();
  </code></pre>