Data Conflict in LINQ

后端 未结 7 2068
不知归路
不知归路 2020-12-13 02:33

When making changes using SubmitChanges(), LINQ sometimes dies with a ChangeConflictException exception with the error message Row not found

7条回答
  •  情歌与酒
    2020-12-13 03:09

    Here's a way to see where the conflicts are (this is an MSDN example, so you'll need to heavily customize):

    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);
            }
        }
    }
    

    To make it ignore the problem and commit anyway:

    db.SubmitChanges(ConflictMode.ContinueOnConflict);
    

提交回复
热议问题