21

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?

And what is the difference between them?

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Flair
  • 2,533
  • 4
  • 15
  • 22
  • In the light of the answer you got - have you even considered, just for a second, looking up the exceptions in the documentation and reading those descriptions? You basically ask to quote the documentation. – TomTom Feb 26 '14 at 08:43
  • 8
    Of course, I read the documentation, but it doesn't make much sense to me, as both of them looks quite similar. 'use it when optimistic concurrency exception can occur'. Hence I gave a situation in my question and asked in that context! – Flair Feb 26 '14 at 08:49
  • I agree with Flair documentation about DbUpdateException, DbUpdateConcurrencyException, OptimisticConcurrencyException and what to catch for manage concurrency exception is not clear at all – Ghini Antonio Mar 13 '17 at 13:40

2 Answers2

18

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

Community
  • 1
  • 1
Gert Arnold
  • 93,904
  • 24
  • 179
  • 256
  • Thank you for your answer, it seems to be most probable case. I'll try out catching both exceptions and see what will happen. Thanks for the link too. – Flair Feb 27 '14 at 16:19
  • 1
    yes, of course all exceptions do not occur due to concurrency but, I'm only concerned about those kind of exceptions (as I mentioned in the question: if data state got changed in the db between you read the data and your `SaveChanges` call). I suppose for my purpose `DbUpdateConcurrency` exception should suffice, or? To be more particular, how do I imitate `ChangeConflictException` from Linq-2-SQL in Entity Framework? – Flair Feb 27 '14 at 16:52
  • 1
    Catching `DbUpdateConcurrencyException` is what you need to handle everything related to optimistic concurrency in EF as configured by timestamp/rowversion columns or `ConcurrencyMode.Fixed` columns (if you're in the DbContext API). See also http://msdn.microsoft.com/en-us/data/jj592904.aspx. I just mentioned `DbUpdateException` because the order of catching is important. – Gert Arnold Feb 27 '14 at 18:22
1

You will get an OptimisticConcurrencyException. Have a look at this.

Now coming to the diffrence.

  • OptimisticConcurrencyException:thrown when an optimistic concurrency violation occurs(suppose more than one perople are changing to the same result,and this will cause the problem of not being sychronized)
  • DbUpdateConcurrencyException:Exception thrown by DbContext when the expected behavior is that SaveChanges for an entity would result in a database update but in fact no rows in the database were affected. This shows that the database has been concurrently updated and a concurrency token that was expected to match did not actually match. State entries referenced by this exception are not serialized due to security and access to the state entries after serialization will return null.
Rohan
  • 613
  • 5
  • 15