Wednesday, July 17, 2024

EF Core 8.0: Numeric Rowversion for Azure SQL/Microsoft SQL Server

In this post, let's have a look at this small yet handy EF Core 8.0 feature for troubleshooting concurrency issues.

Before EF Core 8.0, the rowversion property in C# classes needs to be of type byte[].
public record Post
{
    public int Id { getset}

    public string Title { getset}

    public byte[] Timestamp { getset}
}
And when debugging, it looks like the following.
Timestamp as byte[]
Now with EF Core 8.0, we can map the rowversion to long or ulong.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Post>()
        .Property(e => e.Timestamp)
        .HasConversion<byte[]>()
        .IsRowVersion();
}
public record Post
{
    public int Id { getset}

    public string Title { getset}

    public long Timestamp { getset}
}
When debugging it's more readable now.
Timestamp as long
Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment