Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Concurrency with Multiple Tables/Entities

I understand how to make optimistic concurrency work with Entity Framework for a single table.

But what about the scenario where I want to have optimistic concurrency across multiple tables. To illustrate what I mean, I will use an example:

  • Car table
  • Wheel table, foreign key to Car (multiple wheels per car)

How can I manage concurrency in the case where the ser adds/removes a Wheel from the Car.

Adding a ModifiedTimestamp column in the Car can handle concurrent updates to Car, but what about udpates to the Car's Wheel?

I can only imagine solving this by using pessimistic concurrency (ie - locking the car object to a user) to solve this. But from my research, it seems that pessmistic concurrency is not supported by Entity Framework.

This is just a very simple example, and I'm wondering how everyone solves this type of scenario in general. I can imagine there are many situations where you have an entity, which has related entites, and should all be considered as one in terms of concurrent updates.

like image 947
Mas Avatar asked May 22 '26 16:05

Mas


1 Answers

One way is always modify something in the Car table. Car is aggragetion root for your wheels because wheels cannot exists without Car. If you modify wheel you modify whole aggragated object - car. So add some car update to model this behavior - common is maintaining some column like ModifiedDate.

Pesimistic concurrency is supported by serialized transaction - default isolation level for TransactionScope.

like image 159
Ladislav Mrnka Avatar answered May 24 '26 13:05

Ladislav Mrnka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!