title: Why is DbContext.SaveChanges 10x slower in debug mode
body: <p>Can somebody explain</p>
  
  <ol>
  <li>Why does DbContext.SaveChanges run ~10x slower in debug mode than production mode?</li>
  <li>Is there any way I can speed this up?</li>
  </ol>
  
  <p>In debug mode, my webpage takes 116 seconds to load versus 15 seconds if I start the project without debugging.</p>
  
  <p>I have set trace statements and identified that ~100 of the 116 seconds is spent in my DbContext.SaveChanges method when in debug mode.</p>
  
  <p>Running the project without debugging only 7 seconds in spent in the same section.</p>
  
  <p>Let me know in the comments if you'd like more information..</p>
  
  <p>Project Setup:</p>
  
  <ul>
  <li>ASP.NET webpage</li>
  <li>VS2012</li>
  <li>SQLServer2012</li>
  <li>Entity Framework 5.0</li>
  </ul>
  
  <p>Additional Info: (Let me know in the comments if you need more)</p>
  
  <ul>
  <li>The cumulative number of sql queries over the SaveChanges method is 20,000</li>
  <li>Production Connection String:  Data Source=PC-DEV;Initial Catalog=aspnet-2013-06-04;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFrameworkMUE</li>
  <li>Debug Connection String:  Data Source=PC-DEV;Initial Catalog=aspnet-2013-06-04;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFrameworkMUE</li>
  <li>I've also experienced the same relative performance with LocalDB as the backing database</li>
  </ul>
  
  <p><strong>Update:</strong></p>
  
  <p>As @ruionwriting suggested, I profiled the database and what I found is that the ~20,000 sql commands take exactly the same time whether the project is run in debug or production mode. ( 0 ms per command).</p>
  
  <p>However, the absolute time difference on average between the 20,000 commands is 5ms in debug mode. </p>
  
  <p>Contrasted with production mode, the average time difference over the set of commands is 0.3 ms.</p>
  
  <p>This is the approximate 10x time performance difference and isolates entity framework as what is taking the extra time in debug mode.</p>
  
  <p>Is there a way to configure the debug build such that EntityFramework can be referenced without debugging flags?</p>
  
  <p>And if I were to somehow achieve the performance back through some compiler magic, what would I lose in terms of debugging capabilities? Currently I can't step into entity framework code so I don't think I would miss anything.</p>
  
  <p>Thanks!</p>