问题描述
在Entity Framework(CF,C#)中调用SaveChanges
/SaveChangesAsync
时,如果发生更改冲突(例如,自上次读取以来已更新值),则这两个异常中的哪一个DbUpdateConcurrencyException
或OptimisticConcurrencyException
我应该抓住吗?
When calling SaveChanges
/ SaveChangesAsync
in Entity Framework (CF, C#), if a change conflict occurs (for example, the values has been updated since last read thingy), then which of these two exceptions DbUpdateConcurrencyException
OR OptimisticConcurrencyException
shall I catch?
它们之间有什么区别?
推荐答案
DbUpdateConcurrencyException
是DbContext
抛出的特定异常,因此这是一个可以捕获的异常.该异常可能是由基础OptimisticConcurrencyException
引起的,但如果是这样,则将该异常包装为内部异常.
DbUpdateConcurrencyException
is a specific exception thrown by DbContext
, so this is the one to catch. This exception may be caused by an underlying OptimisticConcurrencyException
, but if so, this exception is wrapped as the inner exception.
并非所有更新异常都是由并发引起的,因此,在捕获DbUpdateConcurrencyException
之后,您还必须捕获DbUpdateException
(因为后者是DbUpdateException
的子类型).
Not all update exceptions are caused by concurrency, so you also have to catch DbUpdateException
after catching DbUpdateConcurrencyException
(because the latter is a subtype of DbUpdateException
).
另请参阅实体框架5.0是否处理乐观并发异常?.
这篇关于实体框架中的并发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!