Concurrency Tokens

Note

This page documents how to configure concurrency tokens. See Handling Concurrency Conflicts for a detailed explanation of how concurrency control works on EF Core and examples of how to handle concurrency conflicts in your application.

Properties configured as concurrency tokens are used to implement optimistic concurrency control.

Configuration

public class Person
{
    public int PersonId { get; set; }

    [ConcurrencyCheck]
    public string LastName { get; set; }

    public string FirstName { get; set; }
}

Timestamp/rowversion

A timestamp/rowversion is a property for which a new value is automatically generated by the database every time a row is inserted or updated. The property is also treated as a concurrency token, ensuring that you get an exception if a row you are updating has changed since you queried it. The precise details depend on the database provider being used; for SQL Server, a byte[] property is usually used, which will be set up as a ROWVERSION column in the database.

You can configure a property to be a timestamp/rowversion as follows:

public class Blog
{
    public int BlogId { get; set; }

    public string Url { get; set; }

    [Timestamp]
    public byte[] Timestamp { get; set; }
}