3

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem I have with TransactionScope is that I'm using the Devart connector to connect with an Oracle database. The TransactionScope is translated by the Devart connector to a distributed transaction, which is not what we want. We want a local transaction because we're only dealing with one database. Also we found some major performance issues with distributed transation. The idea of always updating the root is a good one. Thanks!
What is a wheel is not added to or removed from car, but simply modified (e.g. inflated) - does the car need to be manually marked as "dirty"?
1

Pessimistic concurrency is supported, just place all your data access inside a Transaction Scope.

One simple strategy that you can use is last person to udate the record wins, all other changes are lost. This is not always acceptable.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.