How can we handle concurrency in LINQ?

 Posted by ArticlesMaint on 9/29/2009 | Category: LINQ Interview questions | Views: 2827

LINQ gives three ways by which we can handle concurrency conflicts. To handle concurrency conflicts we need to wrap the LINQ to SQL code in a ‘TRY’ block and catch the ‘ChangeConflictException’. We can then loop through the ‘ChangeConflicts’ collection to specify how we want the conflict to be resolved.

catch (ChangeConflictException ex)

foreach (ObjectChangeConflict objchangeconf in objContext.ChangeConflicts)


There are 3 ways provided by LINQ system to handle concurrency conflicts:-
• KeepCurrentValues :- When this option is specified and concurrency conflicts happen LINQ keeps call the LINQ entity object values as it is and does not push the new values from the database in to the LINQ object.
• OverwriteCurrentValues :- When this option is specified the current LINQ object data is replaced with the database values.
• KeepChanges :- This is the most weird option but can be helpful in some cases. When we talk about classes it can have many properties. So properties which are changed are kept as it is but the properties which are not changed are fetched from the database and replaced.

We need to use the ‘RefereshMode’ to specify which options we need as shown in the below code snippet.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response