dotnet / dotnet/EntityFramework.Docs

Elaborate on concurrency tokens in detached use cases

Open
#860 5 comments 8 reactions 0 assignees View on GitHub
area-optimistic-concurrency
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

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](https://docs.microsoft.com/en-us/ef/core/saving/concurrency)
* Content Source: [entity-framework/core/saving/concurrency.md](https://github.com/aspnet/EntityFramework.Docs/blob/master/entity-framework/core/saving/concurrency.md)
* Product: **entity-framework**
* GitHub Login: @rowanmiller
* Microsoft Alias: **divega**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.