title: EF4 SaveChanges how to avoid out of memory exception
body: <p>The existing fragment</p>
  
  <pre><code>foreach (var ownerCandidates in ownerToCandidatesDictionary)
    {
      foreach (var candidate in ownerCandidates.Value)
      {
         transactionEntities.AddToSomeEntity(someObject)
      }
    }
                  transactionEntities.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
  </code></pre>
  
  <p>Is rewriting to </p>
  
  <pre><code>    int i = 0 ; 
      foreach (var ownerCandidates in ownerToCandidatesDictionary)
      {
           foreach (var candidate in ownerCandidates.Value)
           {
               transactionEntities.AddToSomeEntity(someObject)
            }
            if ( i++ % 1000 == 0 ) 
            {
                 transactionEntities.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
            } 
      }
  
  transactionEntities.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
  </code></pre>
  
  <p>gives us the same functionality in case of successful program termination ? My concern we keep adding, does SaveChanges in a loop work only with what was added since previous SaveChanges. Do we have saving in batches here ?  If it is not the case how the original fragment may be changed to avoid </p>
  
  <pre><code>12/06/2012 7:50:37 PM : System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
     at System.Data.Mapping.Update.Internal.Propagator.Project(DbProjectExpression node, PropagatorResult row, TypeUsage resultType)
     at System.Data.Mapping.Update.Internal.Propagator.Visit(DbProjectExpression node)
     at System.Data.Common.CommandTrees.DbProjectExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
     at System.Data.Mapping.Update.Internal.Propagator.Propagate(UpdateTranslator parent, EntitySet table, DbQueryCommandTree umView)
     at System.Data.Mapping.Update.Internal.UpdateTranslator.&lt;ProduceDynamicCommands&gt;d__0.MoveNext()
     at System.Linq.Enumerable.&lt;ConcatIterator&gt;d__71`1.MoveNext()
     at System.Data.Mapping.Update.Internal.UpdateCommandOrderer..ctor(IEnumerable`1 commands, UpdateTranslator translator)
     at System.Data.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
     at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
     at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
     at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
  </code></pre>