ZZZ Code AI
Report Issues
Suggest Features
Login
EF Core Tools
Chat
Code Explain
More Tools
How is determined when to throw a DbUpdateConcurrencyException in EF Core?
<p>In EF Core, the decision to throw a <code>DbUpdateConcurrencyException</code> is based on the number of affected rows returned. The check for the concurrency token is appended to the <code>WHERE</code> clause, and when performing an <code>UPDATE</code> or <code>DELETE</code>, if no rows were affected, it means there is currently a concurrency conflict.</p> <p>Here is a more detailed answer:</p> <ol> <li><strong>Concurrency Tokens</strong>: When defining your entity model, you can mark a property as a concurrency token: <ul> <li>With the <a href="https://www.learnentityframeworkcore.com/configuration/data-annotation-attributes/concurrencycheck-attribute">[ConcurrencyCheck]</a> data annotation</li> <li>With the <a href="https://www.learnentityframeworkcore.com/configuration/data-annotation-attributes/timestamp-attribute">[TimeStamp]</a> data annoation</li> <li>With the <a href="https://www.learnentityframeworkcore.com/configuration/fluent-api/isconcurrencytoken-method">IsConcurrencyToken()</a> fluent API</li> <li>With the <a href="https://www.learnentityframeworkcore.com/configuration/fluent-api/isrowversion-method">IsRowVersion()</a> fluent API</li> </ul> </li> <li><strong>Database Operations</strong>: When performing an update or delete operation, EF Core includes these concurrency tokens in the SQL <code>WHERE</code> clause.</li> <li><strong>Check the Affected Rows</strong>: After executing the operation, EF Core examines the number of affected rows. <ul> <li>If no rows were affected, it means that no records in the database matched. This is interpreted as a concurrency conflict.</li> </ul> </li> <li><strong>Throw the Exception</strong>: If such a conflict is detected, EF Core throws a <code>DbUpdateConcurrencyException</code>.</li> </ol>
Sponsored by
Entity Framework Extensions
This field is required
A text with a minimum of 10 characters is required
Send
Legal & Licensing
Answer generated by AI may produce inaccurate information about code, people, facts, and more.
Advertising Break!
5
seconds left
Did you know...
That you can now sponsor this project on
GitHub
?