Compartir a través de


Procedimiento para detectar y resolver envíos con conflictos

LINQ to SQL proporciona muchos recursos para detectar y resolver los conflictos que ocasionan los cambios que realizan varios usuarios en la base de datos. Para obtener más información, vea Cómo administrar conflictos de cambios.

Ejemplo

En el ejemplo siguiente se muestra un bloque try/catch que detecta una excepción ChangeConflictException. La información de entidad y miembro de cada conflicto se muestra en la ventana de la consola.

Nota

Debe incluir la directiva using System.Reflection (Imports System.Reflection en Visual Basic) para habilitar la recuperación de la información. Para obtener más información, vea System.Reflection.

// using System.Reflection;
Northwnd db = new Northwnd(@"c:\northwnd.mdf");

Customer newCust = new Customer();
newCust.City = "Auburn";
newCust.CustomerID = "AUBUR";
newCust.CompanyName = "AubCo";
db.Customers.InsertOnSubmit(newCust);

try
{
    db.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException e)
{
    Console.WriteLine("Optimistic concurrency error.");
    Console.WriteLine(e.Message);
    Console.ReadLine();
    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);
        }
    }
}
catch (Exception ee)
{
    // Catch other exceptions.
    Console.WriteLine(ee.Message);
}
finally
{
    Console.WriteLine("TryCatch block has finished.");
}
' Imports System.Reflection

Dim newCust As New Customer()
newCust.City = "Auburn"
newCust.CustomerID = "AUBUR"
newCust.CompanyName = "AubCo"
db.Customers.InsertOnSubmit(newCust)

Try
    db.SubmitChanges(ConflictMode.ContinueOnConflict)

Catch e As ChangeConflictException
    Console.WriteLine("Optimistic concurrency error.")
    Console.WriteLine(e.Message)
    Console.ReadLine()
    For Each occ In db.ChangeConflicts

        Dim metatable As MetaTable = db.Mapping.GetTable(occ.Object.GetType())
        Dim entityInConflict = CType(occ.Object, Customer)
        Console.WriteLine("Table name: {0}", metatable.TableName)
        Console.Write("Customer ID: ")
        Console.WriteLine(entityInConflict.CustomerID)
        For Each mcc In occ.MemberConflicts

            Dim currVal = mcc.CurrentValue
            Dim origVal = mcc.OriginalValue
            Dim databaseVal = mcc.DatabaseValue
            Dim 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)
        Next
    Next

Catch ee As Exception
    ' Catch other exceptions.
    Console.WriteLine(ee.Message)
Finally
    Console.WriteLine("TryCatch block has finished.")
End Try

Consulte también