EntityFramework.Docs icon indicating copy to clipboard operation
EntityFramework.Docs copied to clipboard

Elaborate on concurrency tokens in detached use cases

Open thomasdc opened this issue 7 years ago • 5 comments

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.

thomasdc avatar Jul 24 '18 08:07 thomasdc

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?

sylvaingirardbe avatar Dec 12 '18 07:12 sylvaingirardbe

That's correct in this case. The example code was copy pasted from another documentation page. However, the same principle applies.

thomasdc avatar Dec 12 '18 07:12 thomasdc

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?

VictorioBerra avatar Dec 18 '18 05:12 VictorioBerra

See also https://github.com/aspnet/EntityFrameworkCore/issues/18505

ajcvickers avatar Dec 09 '19 20:12 ajcvickers

See also: https://github.com/dotnet/efcore/issues/27865

ajcvickers avatar Jun 06 '22 10:06 ajcvickers