Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# db.SaveChanges的System.OutOfMemoryException实体框架6_C#_Entity Framework_Entity Framework 6 - Fatal编程技术网

C# db.SaveChanges的System.OutOfMemoryException实体框架6

C# db.SaveChanges的System.OutOfMemoryException实体框架6,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,我正在运行一个批处理作业,用实体框架将事件插入数据库。总规模各不相同,但正常情况下约有350万个活动。此列表被分成60-500k事件组,然后添加到数据库中 然而,当添加了大约一百万个事件后,我得到了System.OutOfMemoryException,必须重新开始。因此,要完成程序,现在必须运行3-4次。调用SaveChanges()后如何清除我的db对象,这样就不会发生这种情况 public DbSet<Events> Events { get; set; } ... var g

我正在运行一个批处理作业,用实体框架将事件插入数据库。总规模各不相同,但正常情况下约有350万个活动。此列表被分成60-500k事件组,然后添加到数据库中

然而,当添加了大约一百万个事件后,我得到了System.OutOfMemoryException,必须重新开始。因此,要完成程序,现在必须运行3-4次。调用
SaveChanges()
后如何清除我的db对象,这样就不会发生这种情况

public DbSet<Events> Events { get; set; }
...
var groupedList = list.GroupBy(x => x.Import.FileName);
var db = new ApplicationDbContext();

foreach (var group in groupedList)
{
   db.Events.AddRange(group);
   db.SaveChanges();
}
公共数据库集事件{get;set;}
...
var groupedList=list.GroupBy(x=>x.Import.FileName);
var db=new ApplicationDbContext();
foreach(groupedList中的var组)
{
db.Events.AddRange(组);
db.SaveChanges();
}
堆栈跟踪:

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.Data.Entity.Core.Mapping.Update.Internal.KeyManager.GetPrincipals(Int32 identifier)
at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand..ctor(TableChangeProcessor processor, UpdateTranslator translator, ModificationOperator modificationOperator, PropagatorResult originalValues, PropagatorResult currentValues, DbModificationCommandTree tree, Dictionary`2 outputIdentifiers)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildInsertCommand(PropagatorResult newRow, TableChangeProcessor processor)
at System.Data.Entity.Core.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode changeNode, UpdateCompiler compiler)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.<ProduceDynamicCommands>d__a.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__58`1.MoveNext()
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateCommandOrderer..ctor(IEnumerable`1 commands, UpdateTranslator translator)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at Project.Program.Main(String[] args) in C:\Users\oscar\Documents\Solution\Project\Program.cs:line 135
System.OutOfMemoryException:引发了类型为“System.OutOfMemoryException”的异常。
位于System.Data.Entity.Core.Mapping.Update.Internal.KeyManager.GetPrinciples(Int32标识符)
位于System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand..ctor(TableChangeProcessor处理器、UpdateTranslator转换器、ModificationOperator ModificationOperator、PropagatorResult原始值、PropagatorResult CurrentValue、DbModificationCommandTree、Dictionary`2 OutputIdentifier)
位于System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildInsertCommand(PropagatorResult newRow,TableChangeProcessor处理器)
位于System.Data.Entity.Core.Mapping.Update.Internal.TableChangeProcessor.CompileCommand(ChangeNode ChangeNode,UpdateCompiler编译器)
在System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.d_ua.MoveNext()中
在System.Linq.Enumerable.d_u58`1.MoveNext()中
位于System.Data.Entity.Core.Mapping.Update.Internal.UpdateCommandOrderer..ctor(IEnumerable`1命令,UpdateTranslator转换器)
位于System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()处
位于System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()处
位于System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.b__2(UpdateTranslator ut)
位于System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult,Func`2 updateFunction)
位于System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()处
位于System.Data.Entity.Core.Objects.ObjectContext.b__35()
在System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 Func,IDBEcutionStrategy executionStrategy,Boolean startLocalTransaction,Boolean releaseConnectionOnSuccess)
位于System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions选项、IDBEcutionStrategy executionStrategy、Boolean startLocalTransaction)
在System.Data.Entity.Core.Objects.ObjectContext.c__DisplayClass2a.b__27()中
在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1操作)中
位于System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions选项,布尔值executeInExistingTransaction)
位于System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions)
位于System.Data.Entity.Internal.InternalContext.SaveChanges()处
在System.Data.Entity.Internal.LazyInternalContext.SaveChanges()中
位于System.Data.Entity.DbContext.SaveChanges()处
在C:\Users\oscar\Documents\Solution\Project\Program.cs中的Project.Program.Main(字符串[]args)处:第135行

插入一个批处理后,应重新创建
DbContext
以清除
ChangeTracker
。这不仅可以解决您的内存问题,还可以让事情变得更快。差不多

foreach (var group in groupedList)
{
   using(var db = new MyDbContext()){
     db.Events.AddRange(group);
     db.SaveChanges();
   }
}

您是否尝试重新创建ApplicationDbContext?这意味着在foreach循环中移动
新的ApplicationDbContext()
。丢弃它并新建一个新的DbContext。可能的重复