title: When should I call SaveChanges() when creating 1000's of Entity Framework objects? (like during an import)
body: <p>I am running an import that will have 1000's of records on each run.  Just looking for some confirmation on my assumptions:</p>
  
  <p>Which of these makes the most sense:</p>
  
  <ol>
  <li>Run <code>SaveChanges()</code> every <code>AddToClassName()</code> call.</li>
  <li>Run <code>SaveChanges()</code> every <em>n</em> number of <code>AddToClassName()</code> calls.</li>
  <li>Run <code>SaveChanges()</code> after <em>all</em> of the <code>AddToClassName()</code> calls.</li>
  </ol>
  
  <p>The first option is probably slow right?  Since it will need to analyze the EF objects in memory, generate SQL, etc.</p>
  
  <p>I assume that the second option is the best of both worlds, since we can wrap a try catch around that <code>SaveChanges()</code> call, and only lose <em>n</em> number of records at a time, if one of them fails.  Maybe store each batch in an List&lt;>.  If the <code>SaveChanges()</code> call succeeds, get rid of the list.  If it fails, log the items.</p>
  
  <p>The last option would probably end up being very slow as well, since every single EF object would have to be in memory until <code>SaveChanges()</code> is called.  And if the save failed nothing would be committed, right?</p>