Concurrency exceptions in Entity Framework

前端 未结 2 1751
-上瘾入骨i
-上瘾入骨i 2021-01-03 21:28

When calling SaveChanges / SaveChangesAsync in Entity Framework (CF, C#), if a change conflict occurs (for example, the values has been updated sin

2条回答
  •  离开以前
    2021-01-03 22:07

    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.

    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).

    See also Entity framework 5.0 handle optimistic concurrency exception?.

提交回复
热议问题