This article is a mirror article of machine translation, please click here to jump to the original article.

View: 3308|Reply: 0

[Source] EF concurrency exception DbUpdateConcurrencyException requeries the cached value

[Copy link]
Posted on 11/1/2023 8:30:43 PM | | | |
Requirements: Use EF optimistic locks to control concurrency, if encountering concurrency, add a retry mechanism, find that the data re-queried after the retry is EF cached data, resulting in always saving exceptions.

The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded. SeeThe hyperlink login is visible.for information on understanding and handling optimistic concurrency exceptions.

This discussion led to thoughts on EF 6.x and EF Core query caching:The hyperlink login is visible.

Handling concurrent Timestamp and ConcurrencyCheck distinction
https://www.itsvse.com/thread-3616-1-1.html

As shown below:



SQL script:

ASP.NET Core 6 backend code:

Controller Code:



Option 1 (recommended)

useEntityState.DetachedMarking the entity state as not being tracked by the context, modify it as follows:

As shown below:



Option 2

Call the Reload() method to refresh the entity object, the code is as follows:

Although reload can also refresh EF cache values,This results in the execution of SQL queries more than onceTherefore, it is not recommended.

The other two methods, AsNoTracking is not suitable, and the other one is not tested.

(End)




Previous:.NET converts the database varbinary string into a byte[] array
Next:OpenWRT settings to boot up the execution command
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com