如何:检索成员冲突信息 (LINQ to SQL)

您可以使用 MemberChangeConflict 类检索有关发生冲突的各成员的信息。 在此上下文中,您可以提供任何成员的冲突的自定义处理方法。 有关更多信息,请参见 开放式并发概述 (LINQ to SQL)

示例

下面的代码循环访问 ObjectChangeConflict 对象。 对于每个对象,它会接着循环访问 MemberChangeConflict 对象。

注意注意

请将 System.Reflection 包含在内以提供 Member 信息。

' 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();
        }
    }
}

请参见

其他资源

如何:管理更改冲突 (LINQ to SQL)