实体框架中的并发异常 [英] Concurrency exceptions in Entity Framework

查看:96
本文介绍了实体框架中的并发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Entity Framework(CF,C#)中调用SaveChanges/SaveChangesAsync时,如果发生更改冲突(例如,自上次读取以来已更新值),则这两个异常中的哪一个DbUpdateConcurrencyExceptionOptimisticConcurrencyException我应该抓住吗?

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?

它们之间有什么区别?

推荐答案

DbUpdateConcurrencyExceptionDbContext抛出的特定异常,因此这是一个可以捕获的异常.该异常可能是由基础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是否处理乐观并发异常?.

这篇关于实体框架中的并发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆