Data Annotations Timestamp Attribute in EF 6 & EF Core

Using Timestamp Attribute is a way to handle the Concurrency issues. The Concurrency issue arises when multiple users attempt to update/delete the same row at the same time.  This issue can be handled either by using the Timestamp column or ConcurrencyCheck attribute on the property. Timestamp columns are the preferred way of using for concurrency check. This attribute is available in both Entity Framework & Entity Framework Core

You can apply Timestamp attribute to any byte array column as shown in the entity model below. The Attribute is applied to RowID Property. The entity framework automatically adds the TimeStamp columns in update/delete queries.  This attribute resides in the namespace system.componentmodel.dataannotations.schema

Timestamp Attribute in Entity Framework
Timestamp Attribute in Entity Framework

Best Entity Framework Core Books

Best Entity Framework Books

  • We can apply Timestamp attribute to only one property in the domain model.
  • The data type of the Timestamp must be a byte array
  • This attribute affects the database as it creates the column with datatype rowversion. byte[]  array without Timestamp creates the varninary(max) column
  • Timestamp columns are the preferred way of using for concurrency check

References

  1. TimestampAttribute 
  2. ConcurrencyCheck Attribute
  3. Data Annotations in Entity Framework & EF Core

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