Optimistic Concurrency Control:?
Short answer: Optimistic Concurrency assumes that conflicts will be rare and allows multiple users to read and modify data without locking it.
Explain a bit more
When updating data, you compare the current data in the database with the data the user fetched earlier (usually by checking a timestamp or version number). If the data has been changed by someone else, you throw a concurrency exception. Steps: Add a timestamp or row version column to the table. When updating, check if the timestamp or row version has changed.
Example code
SqlCommand command = new SqlCommand("UPDATE Customers SET CustomerName = @CustomerName WHERE CustomerID = @CustomerID AND RowVersion = @RowVersion", connection); command.Parameters.AddWithValue("@CustomerName", customerName); command.Parameters.AddWithValue("@CustomerID", customerId); command.Parameters.AddWithValue("@RowVersion", rowVersion); If the RowVersion has changed between the time the user fetched the data and the time they attempt to update, the update will fail, and an exception will be thrown.
Real-world example (ShopNest)
ShopNest’s reporting job still uses ADO.NET for a heavy SQL query where raw performance and stored procedures matter more than EF convenience.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png