Ronnie Overby
Ronnie Overby

Reputation: 46490

Concurrency with Linq To Sql and ASP.NET

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?

Upvotes: 7

Views: 3064

Answers (2)

AndreasN
AndreasN

Reputation: 2897

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.

Upvotes: 2

Christian C. Salvadó
Christian C. Salvadó

Reputation: 828090

Linq To SQL by default supports optimistic concurrency control.

You can change the UpdateCheck property of each field in your table:

In the LINQ to SQL object model, an optimistic concurrency conflict occurs when both of the following conditions are true:

  • The client tries to submit changes to the database.
  • One or more update-check values have been updated in the database since the client last read them.

Upvotes: 0

Related Questions