作法:擷取實體衝突資訊How to: Retrieve Entity Conflict Information

您可以使用 ObjectChangeConflict 類別的物件,提供有關 ChangeConflictException 例外狀況所揭露的衝突資訊。You can use objects of the ObjectChangeConflict class to provide information about conflicts revealed by ChangeConflictException exceptions. 如需詳細資訊, 請參閱開放式平行存取:總覽For more information, see Optimistic Concurrency: Overview.

範例Example

下列範例會逐一查看累積衝突的清單。The following example iterates through a list of accumulated conflicts.

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);
        Console.ReadLine();
    }
}
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 = occ.Object

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

另請參閱See also