作法:擷取成員衝突資訊How to: Retrieve Member Conflict Information

您可以使用 MemberChangeConflict 類別來擷取衝突中各個成員的相關資訊。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.

範例Example

下列程式碼會逐一查看 ObjectChangeConflict 物件。The following code iterates through the ObjectChangeConflict objects. 對於每個物件,它會接著逐一查看 MemberChangeConflict 物件。For each object, it then iterates through the MemberChangeConflict objects.

注意

納入 System.Reflection,以便提供 Member 資訊。Include System.Reflection in order to provide Member information.

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

另請參閱See also