0

My Data

ID   VALUE
1    100

I'm using EFCore and Postgresql. I have 2 requests update a record with Id =1 in the same time.

  • Request 1: Get record Id = 1, recieve {Id =1, Value=100}, then I update Value = Value + 10 and update to DB
  • Request 2: When Request 1 don't finish update, Get record Id = 1, recieve {Id = 1, Value = 100}, then I update Value = Value + 20 and update to DB

Finnaly, In DB have {Id =1, Value= 120}. I expected {Id =1, Value= 130}.

I want when client 2 get data, it must waiting for client finish update that record with new data.

Or when client 2 update data, how can I check data different on DB before Update

Sorry about my English, Thanks for help!

Jennifer S
  • 1,419
  • 1
  • 24
  • 43
tad
  • 11
  • 2
  • check [this question](https://stackoverflow.com/questions/13404061/how-can-i-lock-a-table-on-read-using-entity-framework). It is about the same issue, just different DB server. In short there proposed to lock the record while retrieveing it. Of course there are another ways, to do it in code, e.g. push the action into the single function with thread synchronization in there – ASpirin Jul 29 '20 at 15:21
  • 1
    You can also check out Concurrency Tokens or Time Stamp usage in EF Core: https://learn.microsoft.com/en-us/ef/core/modeling/concurrency?tabs=data-annotations. Also there are some docs on how to handle this: https://learn.microsoft.com/en-us/ef/core/saving/concurrency – Markuzy Jul 29 '20 at 15:28

0 Answers0