Elaborate on concurrency tokens in detached use cases
When you're updating an entity in a detached situation (e.g. MVC or SPA) and want to make use of a concurrency token, it's important to set the OriginalValue of a concurrency token to the CurrentValue as mentioned on https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/concurrency?view=aspnetcore-2.1&tabs=visual-studio. It took me several hours to realise this. It might help others if this situation was elaborated on this page.
// Update the RowVersion to the value when this entity was
// fetched. If the entity has been updated after it was
// fetched, RowVersion won't match the DB RowVersion and
// a DbUpdateConcurrencyException is thrown.
// A second postback will make them match, unless a new
// concurrency issue happens.
_context.Entry(departmentToUpdate)
.Property("RowVersion").OriginalValue = Department.RowVersion;
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 649fbab8-b960-6873-14bf-188d14757fee
- Version Independent ID: f5ee3135-54c8-689c-e28b-b203b376bd6f
- Content: Handling Concurrency Conflicts - EF Core
- Content Source: entity-framework/core/saving/concurrency.md
- Product: entity-framework
- GitHub Login: @rowanmiller
- Microsoft Alias: divega
It seems the example above is incorrect. The concurrency token is LastName and the update that is supposed to trigger the concurrency exception is updating the FirstName. Or am I missing something?
That's correct in this case. The example code was copy pasted from another documentation page. However, the same principle applies.
I also spent way too long today struggling with this until I found a stack overflow post showing Original Value.
Can someone explain why we need to manually manipulate the original value to enable this functionality? Why won't it use whatever value is on the entity?
See also https://github.com/aspnet/EntityFrameworkCore/issues/18505
See also: https://github.com/dotnet/efcore/issues/27865