Procedura: Recuperare informazioni sui conflitti dei membri
È possibile usare la classe MemberChangeConflict per recuperare informazioni sui singoli membri in conflitto. In questo stesso contesto è possibile fornire la gestione personalizzata del conflitto per qualsiasi membro. Per ulteriori informazioni, vedere Concorrenza Ottimistica: Panoramica.
Esempio
Il codice seguente attraversa gli oggetti ObjectChangeConflict. Per ogni oggetto, esegue quindi un'iterazione attraverso gli oggetti MemberChangeConflict.
Nota
Includere System.Reflection per fornire le informazioni su Member.
// 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: {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: {mi.Name}");
Console.WriteLine($"current value: {currVal}");
Console.WriteLine($"original value: {origVal}");
Console.WriteLine($"database value: {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