Skip to content
Advertisement

Why is DbUpdateConcurrencyException thrown?

I am using an SQL server and I have a table whose purpose is to hold a tree-like structure:

The “Path” column value is generated by INSTEAD OF INSERT, UPDATE trigger. I am using EFCore 3.1 and each time I try to add a record into the table, I get DbUpdateConcurrencyException thrown.

What am I missing – how do I fix the problem?

btw, when I disable trigger insert passes and trigger works when I send regular INSERT command.

Thx Panagiotis for your answer. I understand the logic, but it still does not work. I have tried this:

but I get the same answer:

Advertisement

Answer

The problem is the Path.

EF Core uses optimistic concurrency by default, assuming that collisions (ie changes to the same record by another connection) are rare. To ensure the values haven’t changed since a record was loaded, EF Core will check the value of a rowversion column if one exists, or compare all original property values against the table’s values. If Path changes without EF knowing about it, it will appear there was a concurrency conflict.

The best way to fix this is to add a rowversion column to the table and add it to the model with the Timestamp attribute (the old name for rowversion). In SQL Server, a rowversion is updated automatically by the server on every update. This way, only one small binary value is used for the concurrency check :

Another option is to mark only a few of the properties with the ConcurrencyCheck attribute. In this case, every property except Path :

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement