5

I'm migrating code to DotNet Core. I need to resolve a reference to OptimisticConcurrencyException. What NuGet package do I need to load?

Bob.at.Indigo.Health
  • 11,023
  • 13
  • 64
  • 111
  • You are migrating from what target? Where did it use to live? It seems like you are expecting to find an Entity Framework class in Entity Framework Core and it doesn't exist anymore – Camilo Terevinto Jun 11 '18 at 00:58
  • Huh? I'm migrating from .NET 4.6.2 to .NET Core 2.1. If .NET Core 2.1 no longer requires me to catch BOTH `OptimisticConcurrencyException` and `DbUpdateConcurrencyException` then please just tell me so in an Answer to my question. (Bonus points for a reference to some reasonably official documentation that says that 'OptimisticConcurrencyException` is gone in .NET Core.) – Bob.at.Indigo.Health Jun 11 '18 at 01:07
  • Curious that the question is being down-voted, but the answer is attracting up-votes... – Bob.at.Indigo.Health Jun 13 '18 at 23:30

1 Answers1

5

If you're migrating to EF Core, the closest you can get is DbUpdateConcurrencyException.

The general approach to handle a concurrency conflicts is:

  1. Catch DbUpdateConcurrencyException during SaveChanges.
  2. Use DbUpdateConcurrencyException.Entries to prepare a new set of changes for the affected entities.
  3. Refresh the original values of the concurrency token to reflect the current values in the database.
  4. Retry the process until no conflicts occur.

source: https://learn.microsoft.com/en-us/ef/core/saving/concurrency

EDIT:

OptimisticConcurrencyException is in the System.Data.Entity.Core namespace which is part of the EntityFramework (EF6) library.

EF Core is a complete rewrite of the entity framework library so it's highly likely that OptimisticConcurrencyException never went in EF Core.

There was also this thread that suggested to just catch DbUpdateConcurrencyException in EF6. And it was also pointed out that the two exceptions in EF6 just adds confusion. So maybe the EF Core team decided to just implement one over the other.

If still in doubt, create an issue in the EF Core github repo. They're receptive of answering the issues and it might help other users too with the same problem. :)

Jan Paolo Go
  • 5,842
  • 4
  • 22
  • 50
  • Thanks! Yes, having both exceptions was more than confusing. Various docs and examples sometimes referred to only one or the other as the only exception that needed to be caught. From my own code, I know that sometimes I would catch one exception and sometimes I would catch the other. I took the route of always catching both as equivalent exceptions. I'm more than happy to simplify my code and remove the verbose `catch (Exception e) when (e is OptimisticConcurrencyException || e is DbUpdateConcurrencyException)` claptrap in my code. – Bob.at.Indigo.Health Jun 11 '18 at 01:45