dotnet / dotnet/efcore

Table splitting + ConcurrencyToken throws unexpected DBConcurrencyException

Open
#33,750 4 comments 0 reactions 1 assignee Assigned to @AndriySvyryd View on GitHub
area-save-changes customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

When configuring multiple entities with table splitting and row version concurrency tokens, updating the dependents without the principal entity will throw an unexpected `DbUpdateConcurrencyException`.

If the principal entity (`Foo`) is tracked via ChangeTracker, then the updates persist properly. This occurs regardless of whether a transaction is used or not.

EF Configs:

```C#
//Principal
public class Foo
{
public string FooProp { get; set; }
public string FooId { get; set; } = Guid.NewGuid().ToString();

public Bar Bar { get; set; }
public Baz Baz { get; set; }
private class FooConfig : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder.HasKey(p => p.FooId);

builder.HasOne(p => p.Bar).WithOne().HasForeignKey(p => p.FooId);
builder.HasOne(p => p.Baz).WithOne().HasForeignKey(p => p.FooId);

builder.Property("xmin").IsRowVersion();
builder.ToTable("Table");
}
}

}

//Dependent 1
public class Baz
{
public string Property1 { get; set; } = "prop1";
public string FooId { get; set; } = Guid.NewGuid().ToString();

private class BazConfig : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder.HasKey(p => p.FooId);

builder.Property("xmin").IsRowVersion();
builder.ToTable("Table");
}
}

}

//Dependent 2
public class Bar
{
public string Property2 { get; set; } = "prop2";

public string FooId { get; set; } = Guid.NewGuid().ToString();

private class BarConfig : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder.HasKey(p => p.FooId);

builder.Property("xmin").IsRowVersion();
builder.ToTable("Table");

}
}

}

```

![image](https://github.com/dotnet/efcore/assets/112659411/332f27d6-81f0-4af7-bf87-9848e29a4a10)

### Include provider and version information

EF Core version:
Database provider: Postgresql
Target framework: .NET 8
Operating system: WIN 10
IDE:VS 2022

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.