Skip to main content

How can you handle concurrency in LINQ to SQL?

How can you handle concurrency in LINQ to SQL?
Ans. When multiple users are updating the same record at the same time, a conflict will occur. To handle this
concurrency conflicts, LINQ provides you following three ways.
1. KeepCurrentValues - This option will remains the LINQ object values as it is and does not push the new values
from the database in to the LINQ object.
2. OverwriteCurrentValues - This option will replace the LINQ object values with the database values.

3. KeepChanges - In this case changed properties of an object/entity remains as it is but the properties which
are not changed are fetched from the database and replaced.
All these options are available in RefereshMode enum which exist in System.Data.Linq namespace.
To handle concurrency conflicts, wrap the code with in a try block and catch the ChangeConflictException by using
catch block and iterate through the ChangeConflicts collection to resolve the conflict as shown below:
DataContext db = new DataContext();
try
{
// TO DO:
db.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException ex)
{
foreach (ObjectChangeConflict changeconf in db.ChangeConflicts)
{
changeconf.Resolve(RefreshMode.OverwriteCurrentValues);
}

Comments

Popular posts from this blog

Top 10 ASP.NET Web API Interview Questions

What is ASP.NET Web API? ASP.NET Web API is a framework that simplifies building HTTP services for broader range of clients (including browsers as well as mobile devices) on top of .NET Framework. Using ASP.NET Web API, we can create non-SOAP based services like plain XML or JSON strings, etc. with many other advantages including: Create resource-oriented services using the full features of HTTP Exposing services to a variety of clients easily like browsers or mobile devices, etc. What are the Advantages of Using ASP.NET Web API? Using ASP.NET Web API has a number of advantages, but core of the advantages are: It works the HTTP way using standard HTTP verbs like  GET ,  POST ,  PUT ,  DELETE , etc. for all CRUD operations Complete support for routing Response generated in JSON or XML format using  MediaTypeFormatter It has the ability to be hosted in IIS as well as self-host outside of IIS Supports Model binding and Validation Support for OD...

First, FirstOrDefault, Single, SingleOrDefault In C#

For people who are new to LINQ, it is difficult to understand the difference between First, FirstOrDefault, Single, SingleOrDefault. In this blog, I will explain what to use and when.     I will take a simple example to make you understand practically how these methods work.   Consider a class Employee with properties as Id, Name, and Department. class  Employee {    public   int  Id {  get ;  set ; }    public   string  Name {  get ;  set ; }    public   string  Department{  get ;  set ; } } I have a list of Employees: List<Employee> employeeList =  new  List<Employee>(){    new  Employee() { Id = 1, Name =  "Sunny" , Department =  "Technical"  },    new  Employee() { Id=2, Name= "Pinki" , Department = "HR" },    new  Employee() { Id=3, Name= "Tensy" , De...

Extension methods in C#

Consider the class C# 1 2 3 4 5 6 7 8 9 10 11 12 13          namespace ExtensionMethod      {          public class testClass {              public string sayHello ( ) {              return "Hello" ;            }        }      }     Invoke the above from your form using C# 1 2 3 4 5 6          testClass test = new testClass ( ) ;      MessageBox . Show ( test . sayHello ( ) ) ;     This will show “Hello” in message box. Consider the scenario where you don...