C# 为什么不是';t savechanges同步保存我所有的更改?
我想我可能在理解这应该是如何工作的过程中遗漏了一些东西。我有一些导入文件的代码。它循环遍历每个记录,进行一些处理,然后通过C# 为什么不是';t savechanges同步保存我所有的更改?,c#,.net,task-parallel-library,async-await,entity-framework-6,C#,.net,Task Parallel Library,Async Await,Entity Framework 6,我想我可能在理解这应该是如何工作的过程中遗漏了一些东西。我有一些导入文件的代码。它循环遍历每个记录,进行一些处理,然后通过DbContext实例将该记录添加到表中 我初始化DbContext如下: protected void ResetDbContext() { if (_db != null) _db.Dispose(); _db = new AppDBEntities(); _db.Configuration.AutoDetectChangesEn
DbContext
实例将该记录添加到表中
我初始化DbContext
如下:
protected void ResetDbContext()
{
if (_db != null)
_db.Dispose();
_db = new AppDBEntities();
_db.Configuration.AutoDetectChangesEnabled = false;
_db.Configuration.ValidateOnSaveEnabled = false;
}
foreach (var rec in engine)
{
var record = new CommonImportRecord(rec);
ProcessRecord(record, options, index);
index++;
}
_db.SaveChanges();
protected async Task<int> ProcessRecord(CommonImportRecord record, ImportOptions options, int index)
{
DisplayProcessStatus(index, options);
// Code removed which fills in various properties of the record
_db.MyTable.Add(record);
if (index % options.UpdateInterval == 0)
{
return await _db.SaveChangesAsync();
// This was originally here, commented out when I changed SaveChanges() to SaveChangesAsync()
// ResetDBContext();
}
}
我的主循环如下所示:
protected void ResetDbContext()
{
if (_db != null)
_db.Dispose();
_db = new AppDBEntities();
_db.Configuration.AutoDetectChangesEnabled = false;
_db.Configuration.ValidateOnSaveEnabled = false;
}
foreach (var rec in engine)
{
var record = new CommonImportRecord(rec);
ProcessRecord(record, options, index);
index++;
}
_db.SaveChanges();
protected async Task<int> ProcessRecord(CommonImportRecord record, ImportOptions options, int index)
{
DisplayProcessStatus(index, options);
// Code removed which fills in various properties of the record
_db.MyTable.Add(record);
if (index % options.UpdateInterval == 0)
{
return await _db.SaveChangesAsync();
// This was originally here, commented out when I changed SaveChanges() to SaveChangesAsync()
// ResetDBContext();
}
}
ProcessRecord
看起来像这样:
protected void ResetDbContext()
{
if (_db != null)
_db.Dispose();
_db = new AppDBEntities();
_db.Configuration.AutoDetectChangesEnabled = false;
_db.Configuration.ValidateOnSaveEnabled = false;
}
foreach (var rec in engine)
{
var record = new CommonImportRecord(rec);
ProcessRecord(record, options, index);
index++;
}
_db.SaveChanges();
protected async Task<int> ProcessRecord(CommonImportRecord record, ImportOptions options, int index)
{
DisplayProcessStatus(index, options);
// Code removed which fills in various properties of the record
_db.MyTable.Add(record);
if (index % options.UpdateInterval == 0)
{
return await _db.SaveChangesAsync();
// This was originally here, commented out when I changed SaveChanges() to SaveChangesAsync()
// ResetDBContext();
}
}
受保护的异步任务ProcessRecord(CommonImportRecord记录、ImportOptions、int索引)
{
显示进程状态(索引、选项);
//已删除填充记录各种属性的代码
_db.MyTable.Add(记录);
如果(索引%options.UpdateInterval==0)
{
return wait_db.SaveChangesAsync();
//当我将SaveChanges()更改为savechangessync()时,注释掉了原来的内容
//ResetDBContext();
}
}
我对savechangessync()
所做的唯一真正更改是添加async Task
作为ProcessRecord
的返回类型,将SaveChanges()
更改为return wait savechangessync()
,并注释掉对ResetDBContext的调用。
在异步更改之前,一切正常。之后,似乎并没有保存我的所有记录
我错过了什么 您正在调用一个
async
方法,该方法在不等待任务完成的情况下返回任务。在转到下一条记录之前,需要使用wait
异步等待。将async
方法命名为“async”后缀也是一个标准:
要添加到@l3arnon的答案中,您可以将状态机的生成保存在
ProcessRecordAsync
中:
这:
protected async Task ProcessRecordAsync(CommonImportRecord记录、导入选项、int索引)
{
//为简洁起见删除了代码
return wait_db.SaveChangesAsync();
}
可以转化为:
protected Task<int> ProcessRecordAsync(CommonImportRecord record, ImportOptions options, int index)
{
// Removed code for brevity
return _db.SaveChangesAsync();
}
受保护的任务进程RecordAsync(CommonImportRecord记录、ImportOptions、int索引)
{
//为简洁起见删除了代码
返回_db.SaveChangesAsync();
}
因为在调用ProcessRecordAsync
时,实际上没有使用savechangesync
的返回值