-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to compare entity new values with current database values? #26839
Comments
Optimistic concurrency like this is the typical pattern for dealing with this situation, regardless of whether you are using EF or not.
Documentation is here: Handling Concurrency Conflicts. Can you provide specifics on the issues you are having or why you feel it is unintuitive? |
Problem what I see is in solution which I describe. What I need is how to compare values in database with entity. How this solution do it is about change concurrency token property before SaveChanges. It throws ConcurrencyException because concurrency token property has different value from database. Then I save entity and reload it from database and restore state which I save before reloading but concurrency token property. It means I have entity with database state and I can create compare changes to database state and not when entity was loaded first time. I don't think Optimistic Locking or Concurrency Conflicts in EF is not intuitive. I describe solution which use optimistic locking to compare changes with database value - it is, I think, nonintuitive use of concurrency token. Is it more clear what I mean? |
@Zefek The only way you can reliably do this is to start a transaction before reading the value for Anything else involves a race condition between the read and the update. This is why "optimistic concurrency" was invented. Instead of pessimistically doing everything in a transaction since something else might change it while we're working with it, we instead optimistically assume that we're the only one making a change, and then deal with the (hopefully) rare case where two changes are made concurrently. |
@ajcvickers Thank you very much. I didn't know about SELECT FOR UPDATE. I know how optimistic concurrency works. But in solution what I mentioned concurrency token is changed each time before SaveChanges so concurrency exception is thrown even if no one changes record. |
Hello,
I would like to ask about data comparison between entity and database state. Reason why I would ask about it is log some changes. For example: I have two users which load customer entity in same time from database. It means both users have customer with name Test. User A changes customer name to TestA and user B changes customer name to TestB. User A saves entity into database first so there is some audit record "User A changed customer name from Test to TestA".
When User B wants to save I compare old and new values from Tracker and I get audit record "User B changed customer name from Test to TestB". It is allright.
But is there possibility to get record for audit "User B changed customer name from TestA to TestB" by EF because TestA value is in database?
I saw some solution when entity has ConcurrencyToken property which was changed before save so SaveChanges end with ConcurrencyException. In catch block there was code that entity properties (state) were saved, entity was reloaded and then entity was recovered by saved properties but concurrency token property and then SaveChanges was called again.
I think solution with concurrency token could work but it is not good because nobody knows how it works after two years of development and it is very nonintuitive. Now there is requirement user wants to know there is another user which changed data before but concurrency token is used for another reason.
I know about system-versioned (temporal) tables on MSSQL (temporal tables are not approved by customer because of fear of database size and performance). But is there another possibility how to compare entity with database state before save changes to database through EF? Or should audit log be solved on database level?
Thank you.
The text was updated successfully, but these errors were encountered: