Glory to Russia
Glory to Russia

Reputation: 18730

How to avoid invalid concurrent modifications in EF 4

I have a 3-tier application:

  1. Client application
  2. Server application
  3. Database server

The server uses Entity Framework 4 to read and write data to/from the database.

Imagine following situation:

  1. Client application creates an instance of an entity with a RowVersion property. At this point in time, the property is equal to null.
  2. Client application sends a request "Save this instance in the database" to the server.
  3. Server saves the object in the database and automatically sets the value of the RowVersion property. But at the client side, that value is still equal to null.
  4. Client application modifies the object it created in the first step, sends a request to the server and the server gets a concurrency exception when trying to save the new version of the object.

Are there any standard mechanisms for solving this type of problem?

Upvotes: 0

Views: 114

Answers (1)

Evgeniy Labunskiy
Evgeniy Labunskiy

Reputation: 2042

I dont know how the system works inside (think communication between Client and Server goes using some API). As I see you trying to handle the situation when 2 clients modifying same entity and you need to notify the client if he is trying to save the version that is older that current.

So I will do next:

  • On step 3 server must return the version ID (first save of entity)
  • Next modification of entity by client will have a version id and you need to check if current version id is equal or older than you have on server (think that rowversion is timestamp)
  • Server logic will handle this states and send to client response: saved a new version (if current version is equal and will send back new version id) or false state if version is older (other client made modification already).

This is it in simplified way.

[Update]

Looks like in this article you will find the implementation that is very close to your needs: http://weblogs.asp.net/ricardoperes/archive/2012/05/28/yet-another-asp-net-mvc-crud-tutorial.aspx

Upvotes: 1

Related Questions