Question

I am looking at using Entity Framework 6 for a new project.

When we develop our tables we add an update counter of type int, and when performing an update we check that it hasn't changed and increment it on a successful update.

From what I've read I can do the check by adding a ConcurrencyCheck attribute to the update counter property, but how do I perform the increment on successful update?

Or should I just use a Rowversion column instead?

Était-ce utile?

La solution

This Microsoft blog post contains links to a number of EF6 and MVC guides. There is one in particular you should check out - Handling Concurrency with the Entity Framework 6. The guide is using an ASP.Net MVC project, but the database principles should apply to almost any type of project.

In short, though, if you use a ConcurrencyCheck attribute then you don't have to update any counter yourself - but it adds more overhead to your application's database operations because under the hood EF will be extrapolating every SQL query to include concurrency checks for properties that you have applied the ConcurrencyCheck attribute to. If you have a very large table (lots of columns), this can cause slower application performance.

The better way would be for you to use TimeStamp attribute on a byte array property in your code-first class (or if you are using Fluent API you set IsConcurrencyToken() on the property). Then, if there is a concurrency conflict EF will throw a DbUpdateConcurrencyException - it's up to you to then catch it and handle it accordingly.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top