Throw a better exception message when concurrency token is an array

Open
#18,505 34 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
database

Research direction

Use the supplied SQL Server RowVersion reproduction as the starting point; compare assigning a new byte array with mutating the tracked array, and verify behavior against the InMemory provider. Done should be defined by the intended concurrency failure and exception message, but the issue does not name files or tests.

Written by the indexing model from the issue text.

Description

area-save-changes customer-reported

Concurrency check doesn't work when using RowVersion (SQL Server's timestamp) and one creates a new instance of byte array, instead of updating existing.
Happens when using SQL Server store, but not with InMemory. See code below.

Steps to reproduce
// entity
public class Party
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public byte[] RowVersion { get; set; }
    }

// mapping excerpt
modelBuilder.Entity<Party>(entity =>
{
    entity.ToTable("party");
    entity.Property(e => e.Id).HasColumnName("id");
    entity.Property(e => e.FirstName)
    .HasColumnName("first_name")
    .HasMaxLength(255);
    entity.Property(e => e.RowVersion)   // maps to timestamp SQL server type
    .IsRequired()
    .HasColumnName("row_version")
    .IsRowVersion();
}

// this code doesn't raise an exception at all
var party = db.Party.First();
party.RowVersion = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
db.Update(party);
db.SaveChanges();  // <- error, no exception is thrown

// this code correctly raises a concurrency exception
var party = db.Party.First();
party.RowVersion[0] = 99;
db.Update(party);
db.SaveChanges();  // throws dbconcurrencyexception
Further technical details

EF Core version: 3.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer (works correctly with InMemory)
Target framework: .NET Core 3.0
Operating system: Windows 10

Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.