现有片段
foreach (var ownerCandidates in ownerToCandidatesDictionary)
{
foreach (var candidate in ownerCandidates.Value)
{
transactionEntities.AddToSomeEntity(someObject)
}
}
transactionEntities.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
正在重写
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);
在程序成功终止的情况下为我们提供相同的功能?我们不断添加我的担忧是,循环中的 SaveChanges 是否仅适用于自上一次 SaveChanges 以来添加的内容。我们这里有批量保存吗?如果不是这种情况,如何更改原始片段以避免
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.<ProduceDynamicCommands>d__0.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>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)