0

What's the idiomatic way to implement optimistic concurrency in Entity Framework Database First if you have navigation properties on your entities (Ie x to Many relationship between parent and child tables)? Do you need to check the rowversion on both parent and child entities or is there wa such that only checking the parent entity's rowversion would suffice to know whether any child table was updated as well as any parent?

Consider 2 tables:

Table Parent {
   ParentId INT PRIMARY KEY,
   ValueParent INT
   Version ROWVERSION
}


Table Child {
   ParentId INT FOREIGN KEY
   ChildId INT,
   ValueChild INT
   Version ROWVERSION
  (ParentId,ChildId) Primary Key
}

When converted to entities, the tables will look like the following:

class Parent{
   int ParentId;
   int valueParent;
   byte[] version;
   virtual ICollection<Child> Children
}

class Child{
   int parentId;
   int childId;
   int valueChild;
   byte[] version;
}

If I load a Parent object, and only update the child entity on it,

parent.Children.ElementAt(0).valueChild = 1 //

In the db, the rowversion in the child entity will increment, but the parent rowversion will remain unchanged. Is that valid? If I want to implement optimistic concurrency I'd still need to do a rowversion check on both parent and child to ensure that the model hasn't changed. I could set concurrency mode to fixed for versions on both entities but that wouldn't prevent having to check at the beginning before I do the update right?

What's the idiomatic way to implement optimistic concurrency if you have 1-many relationships between your entities? Is there some way to do it where I only need to check the parent rowversion or would I need always to check rowversions of any child elements as well?

3
  • If you want to treat child rows as part of a unit-of-work, I'd tell EF that your parent object has been modified. Mar 28, 2021 at 23:30
  • Through setting the state on the Entity? context.Entry(parentEntity).State = EntityState.Modified; This would force an update of the parent rowversion even none of the properties were changed?
    – pingOfDoom
    Mar 29, 2021 at 0:12
  • Right, sometimes db row concurrency doesn't map well to a logical unit-of-work. One user changes order line 1, another changes order line 3... Making sure you always update some single database row can help there. Mar 29, 2021 at 0:37

1 Answer 1

1

Optimistic concurrency is checked on every row, and you just need to configure the column as a Timestamp/Rowversion and EF will generate the concurrency checks for you.

Is there some way to do it where I only need to check the parent rowversion

As @Jeremy Lakeman suggests, if you always update the parent when any child changes, then concurreny violations from your EF app can be detected and prevented. But it wouldn't protect against ad-hoc updates to the child rows.

3
  • How do you mean ad-hoc? Like other transactions on the child tables or manual SQL updates to those rows?
    – pingOfDoom
    Mar 29, 2021 at 14:38
  • By anything that doesn’t modify the parent. Could be other EF transactions or a TSQL batch job, etc. Mar 29, 2021 at 14:43
  • 1
    Thanks! My takeaway was that we always need to update the parent row, which is fine for my application constraints
    – pingOfDoom
    Apr 5, 2021 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.