在处理context.SaveChanges()
期间的多个潜在异常时,其中一个异常是OptimisticConcurrency
。微软在http://msdn.microsoft.com/en-us/library/bb399228.aspx中对EF 4.x进行了讨论...
try
{
// Try to save changes, which may cause a conflict.
int num = context.SaveChanges();
Console.WriteLine("No conflicts. " +
num.ToString() + " updates saved.");
}
catch (OptimisticConcurrencyException)
{
// Resolve the concurrency conflict by refreshing the
// object context before re-saving changes.
context.Refresh(RefreshMode.ClientWins, orders);
// Save changes.
context.SaveChanges();
Console.WriteLine("OptimisticConcurrencyException "
+ "handled and changes saved");
}
......但是在EF 5.0(RC)中,这似乎不起作用,因为Refresh()
在我的EF5,基于代码优先的DbContext
类派生的context
类上不存在。
我看到context.Entry(context.SalesOrderHeaders).Reload();
- 但这似乎只是直接从数据库重新加载,并不是刷新/合并(使用客户端胜利策略)。
有什么方法来处理EF5中的乐观并发异常吗? 实际上,关于SaveChanges()中异常处理的一般指针也很好。
谢谢
Single(...)
时,我想通过catch (DbUpdateConcurrencyException ex){foreach (var entry in ex.Entries){entry.OriginalValues.SetValues(entry.GetDatabaseValues());}
迭代所有失败来实现。您认为呢?此外,“不要触摸保存更改的当前值”对我来说不是很清楚:(..似乎两者都会获取dB值? - DeepSpace101