I have a parent entity that I need to do a concurrency check (as annotated as below)
[Timestamp]
public byte[] RowVersion { get; set; }
I have a bunch of client processes that access readonly values out of this parent entity and primarily update its child entities.
The constraint
Clients should not interfere with each other's work, (e.g. updating child records should not throw a concurrency exception on the parent entity).
I have a server process that does update this parent entity, and in this case the client process needs to throw if the parent entity has been changed.
Note : The client's concurrency check is sacrificial, the server's workflow is mission critical.
The problem
I need to check (from the client process) if the parent entity has changed without updating the parents entity's row version.
It's easy enough to do a concurrency check on the parent entity in EF:
// Update the row version's original value
_db.Entry(dbManifest)
.Property(b => b.RowVersion)
.OriginalValue = dbManifest.RowVersion; // the row version the client originally read
// Mark the row version as modified
_db.Entry(dbManifest)
.Property(x => x.RowVersion)
.IsModified = true;
The IsModified = true
is the deal breaker because it forces the row version to change. Or, said in context, this check from the client process will cause a row version change in the parent entity, which interferes needlessly with the other client processes' workflows.
A work around : I could potentially wrap the SaveChanges
from the client process in a Transaction and then a subsequent read of the parent entity's row version, in-turn, rolling back if the row version has changed.
Summary
Is there an out-of-the-box way with Entity Framework where I can SaveChanges
(in the client process for the child entities) yet also check if the parent entity's row version has changed (without updating the parent entities row version).