zonder
zonder

Reputation: 253

Problem with ConcurrencyCheck attribute approach in EF

I've found two ways of concurrency checking for my entities in EF 4.1:

The first one is very simple. You just mark byte array property as TimeStamp, create additional column in database and voila...

I've got a problem with the second method. Enity Framework has started generate sql script for concurrency check, when I marked the LastUpdateDate property.

Property:

[ConcurrencyCheck]
public DateTime LastUpdateDate { get; set; }

Sql:

select
...
where (([Id] = @3) and ([LastUpdateDate] = @4))
...
@4='value'

But EF does not generate sql script for updating the value of LastUpdateDate?

Is it possible to say EF to update the LastUpdateDate after concurrency checking without triggers or something like this?

And the second question: What is the best practice of using concurrency checking in EF when you have something like LastUpdateDate property(property will be displayed in UI)? Is it better to check concurency using LastUpdateDate and avoid creating of addtional column for TimeStamp in your tables or create additional TimeStamp property and renounce of the using DateTime property for concurrency checking?

Upvotes: 4

Views: 5110

Answers (1)

Alexandre Brisebois
Alexandre Brisebois

Reputation: 6743

Have you tried to use a rowversion (timestamp) instead of the DateTime datatype to check for concurency?

I would use the timestamp, because you are sure that the system will update it for you. Further more the value will be very precice.

The following blog posts will give you more information about how to map a timestamp. The first one shows how to use the timestamp as a concurrency check.

Code First Optimistic Concurrency with Fluent Assertions

Round tripping a timestamp field with EF4.1 Code First and MVC 3

Upvotes: 3

Related Questions