다음을 통해 공유


방법: 전송 충돌 검색 및 해결

LINQ to SQL에서는 여러 사용자에 의한 데이터베이스 변경으로 발생하는 충돌을 검색하고 해결하는 많은 리소스를 제공합니다. 자세한 내용은 방법: 변경 충돌 관리를 참조하세요.

예시

다음 예제에서는 ChangeConflictException 예외를 catch하는 try/catch 블록을 보여 줍니다. 각 충돌의 엔터티 및 멤버 정보는 콘솔 창에 표시됩니다.

참고 항목

정보 검색을 지원하려면 using System.Reflection 지시문(Visual Basic의 Imports System.Reflection)을 포함해야 합니다. 자세한 내용은 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

참고 항목