Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

How to: Detect and Resolve Conflicting Submissions (LINQ to SQL)

LINQ to SQL provides many resources for detecting and resolving conflicts that stem from multi-user changes to the database. For more information, see How to: Manage Change Conflicts (LINQ to SQL).

The following example shows a try/catch block that catches a ChangeConflictException exception. Entity and member information for each conflict is displayed in the console window.

NoteNote

You must include the using System.Reflection directive (Imports System.Reflection in Visual Basic) to support the information retrieval. For more information, see System.Reflection.


' Imports System.Reflection

Dim newCust As New Customer()
newCust.City = "Auburn"
newCust.CustomerID = "AUBUR"
newCust.CompanyName = "AubCo"
db.Customers.InsertOnSubmit(newCust)

Try
    db.SubmitChanges(ConflictMode.ContinueOnConflict)

Catch e As ChangeConflictException
    Console.WriteLine("Optimistic concurrency error.")
    Console.WriteLine(e.Message)
    Console.ReadLine()
    For Each occ In db.ChangeConflicts

        Dim metatable As MetaTable = db.Mapping.GetTable(occ.Object.GetType())
        Dim entityInConflict = CType(occ.Object, Customer)
        Console.WriteLine("Table name: {0}", metatable.TableName)
        Console.Write("Customer ID: ")
        Console.WriteLine(entityInConflict.CustomerID)
        For Each mcc In occ.MemberConflicts

            Dim currVal = mcc.CurrentValue
            Dim origVal = mcc.OriginalValue
            Dim databaseVal = mcc.DatabaseValue
            Dim mi = mcc.Member
            Console.WriteLine("Member: {0}", mi.Name)
            Console.WriteLine("current value: {0}", currVal)
            Console.WriteLine("original value: {0}", origVal)
            Console.WriteLine("database value: {0}", databaseVal)
        Next
    Next

Catch ee As Exception
    ' Catch other exceptions.
    Console.WriteLine(ee.Message)
Finally
    Console.WriteLine("TryCatch block has finished.")
End Try


Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.