I try to use optimistic concurrency check in EF Core with SQLite.
The simplest positive scenario (even without concurrency itself) gives me
Microsoft.EntityFrameworkCore.
This is because you use Guid
:
public Guid Id { get; set; }
This issue is discussed and reproduced in Gitub:
The error here is due to ApplicationUser.ConcurrencyStamp property. ApplicationUser in identity uses ConcurrencyStamp of type Guid for concurrency. When creating new class it sets the value to NewGuid(). When you create new ApplicationUser like that and set its state to Modified EF Core does not have data about what was ConcurrencyStamp in database. Hence it will use whatever is the value set on the item (which will be NewGuid()) Since this value differ from value in database and it is used in where clause of update statement, exception is thrown that 0 rows modified when expected 1.
When updating entity with concurrency token you cannot create new object and send update directly. You must retrieve record from database (so that you have value of ConcurrencyStamp) then update the record and call SaveChanges. Since the ApplicationUser.ConcurrencyStamp is client side concurrency token you also need to generate a NewGuid() while updating the record. So it can update the value in database.
Find more info about how to deal with ApplicationUser.ConcurrencyStamp
here.