views:

888

answers:

2

What is the best way to check for concurrency issues when using LINQ to SQL in an ASP.net application.

In my application, I am retrieving a record from the database and displaying the fields in editable textboxes. Then the datacontext is thrown away.

How should I save the entity object so that I can use L2Sql's built in concurrency features? I can't save the object in session and reattach it to a new datacontext: l2s complains that the object is not new.

The LinqDataSource manages to do this somehow. Does anyone know how?

A: 
CMS
Yes, I understand all of that.
Ronnie Overby
Their problem is that they lose the DataContext on postback, and you can't attach the original object a new DC.
Lucas
+2  A: 

The way the optimistic concurrency in Linq2Sql works is that it stores the original values and compares them on insert.

If you throw the datacontext away you loose the original values.

What i usually do is to load the object form the database once more when i'm about to save, then modify that object with the values from the form.

AndreasN
Yes, but if I do that (which I was before) any other users' changes will be loaded and Linq to sql will not throw a concurrency error.
Ronnie Overby
have you tried looking at the Table<TEntity>.Attach(TEntity entity, TEntity original) method?
AndreasN
How does that differ from just getting the object from the database and making the changes and doing SubmitChanges()?
Ronnie Overby
This works, but only when I DONT have a timestamp column (it checks every field). What if I want to use the timestamp column? Here is the error: Value of member 'Version' of an object of type 'Faculty' changed.A member that is computed or generated by the database cannot be changed.
Ronnie Overby
Just compare the timestamp value instead of overwritting it
Orlangur