Configuring timestamp tokens through Fluent API

The Fluent API configuration is made possible because Microsoft has exposed such functionality against any property in the model. In our blogging system, we will configure the Timestamp of the Post entity as a concurrency token by enabling the property using IsConcurrencyToken() along with ValueGeneratedOnAddOrUpdate(). The following code would add a new property Timestamp as discussed in this section:

    public class Post 
{
// Code removed for brevity
public byte[] Timestamp { get; set; }
}

We have revisited the Post type and included the byte array timestamp property that will be used in the Fluent API configuration. The following configuration code would enforce the property value to be added or updated during persistence and marked as a concurrency token:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Code removed for brevity
modelBuilder.Entity<Post>()
.ToTable("Post")
.Property(x=>x.Timestamp)
.ValueGeneratedOnAddOrUpdate()
.IsConcurrencyToken();
// Code removed for brevity
}

The preceding configuration will let EF Core consider the Timestamp column as the concurrency token, and any further update to this column will restrict users from performing parallel updates, as it did in the data annotation configuration.

We have exhaustively seen how this concurrency token works, so let's move on to the next section, which explores how concurrency conflicts were handled.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset