Data Annotations ConcurrencyCheck EF Core

Concurrency Check attribute EF Core is used to handle conflicts that result when multiple users are updating (or deleting) the table at the same time. You can add the ConcurrencyCheck attribute on any property, which you want to participate in the Concurrency Check.

What is Concurrency check

Assume that two users simultaneously query for same data to edit from the Employee Table. One of the users saves his changes. Now, the other user is now looking at the data, which is invalid. If he also modifies the data and saves it, it will overwrite the first user’s changes. What if both users save the data at the same time. We never know which data gets saved.

We use the Concurrency check precisely to avoid such situations. To do that we include additional fields in the where clause apart from the primary key. For Example, by including the name field in the where clause, we are ensuring that the value in the name field has not changed since we last queried it. If someone has changed the field, then the where clause fails the Entity Framework raises the exception

ConcurrencyCheck  Attribute

This attribute resides in the System.ComponentModel.DataAnnotations namespace

Best Entity Framework Core Books

Best Entity Framework Books

In the above example, We decorate the Name Property with the ConcurrencyCheck attribute. When Entity Framework generates an update or delete statement, it always includes the Name column in where clause.

This attribute does not affect the database mapping in any way. This attribute is similar to timestamp attribute.

  • You can apply ConcurrencyCheck Attribute on any number of properties.
  • There is no restriction on a data type for this attribute

References

  1. ConcurrencyCheckAttribute

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top