Share via


How to: Retrieve Member Conflict Information (LINQ to SQL)

You can use the MemberChangeConflict class to retrieve information about individual members in conflict. In this same context you can provide for custom handling of the conflict for any member. For more information, see Optimistic Concurrency Overview (LINQ to SQL).

Example

The following code iterates through the ObjectChangeConflict objects. For each object, it then iterates through the MemberChangeConflict objects.

Note

Include System.Reflection in order to provide Member information.

' Add 'Imports System.Reflection' for this section.
Dim db As New Northwnd("...")
'...
Try
    db.SubmitChanges(ConflictMode.ContinueOnConflict)

Catch ex As ChangeConflictException
    Console.WriteLine("Optimistic concurrency error.")
    Console.WriteLine(ex.Message)
    For Each occ As ObjectChangeConflict In db.ChangeConflicts
        Dim metatable As MetaTable = db.Mapping.GetTable(occ.Object.GetType)
        Dim entityInConflict As Object = occ.Object

        Console.WriteLine("Table name: " & metatable.TableName)
        Console.Write("Customer ID: ")
        Console.WriteLine(entityInConflict.CustomerID)

        For Each mcc As MemberChangeConflict In occ.MemberConflicts
            Dim currVal = mcc.CurrentValue
            Dim origVal = mcc.OriginalValue
            Dim databaseVal = mcc.DatabaseValue
            Dim mi As MemberInfo = mcc.Member

            Console.WriteLine("Member: " & mi.Name)
            Console.WriteLine("current value: " & currVal)
            Console.WriteLine("original value: " & origVal)
            Console.WriteLine("database value: " & databaseVal)
            Console.ReadLine()
        Next
    Next
End Try
// Add 'using System.Reflection' for this section.
Northwnd db = new Northwnd("...");

try
{
    db.SubmitChanges(ConflictMode.ContinueOnConflict);
}

catch (ChangeConflictException e)
{
    Console.WriteLine("Optimistic concurrency error.");
    Console.WriteLine(e.Message);
    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
    {
        MetaTable metatable = db.Mapping.GetTable(occ.Object.GetType());
        Customer entityInConflict = (Customer)occ.Object;
        Console.WriteLine("Table name: {0}", metatable.TableName);
        Console.Write("Customer ID: ");
        Console.WriteLine(entityInConflict.CustomerID);
        foreach (MemberChangeConflict mcc in occ.MemberConflicts)
        {
            object currVal = mcc.CurrentValue;
            object origVal = mcc.OriginalValue;
            object databaseVal = mcc.DatabaseValue;
            MemberInfo 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);
            Console.ReadLine();
        }
    }
}

See Also

Other Resources

How to: Manage Change Conflicts (LINQ to SQL)