问题描述
在使用 SubmitChanges()
进行更改时,LINQ 有时会因 ChangeConflictException
异常而终止,并显示错误消息 Row not found or changed
,而没有当另一个用户更改了该行中的某些数据时,任何表明发生冲突的行或发生冲突的更改的字段的任何指示.
When making changes using SubmitChanges()
, LINQ sometimes dies with a ChangeConflictException
exception with the error message Row not found or changed
, without any indication of either the row that has the conflict or the fields with changes that are in conflict, when another user has changed some data in that row.
有什么方法可以确定哪一行有冲突以及它们出现在哪些字段中,还有什么方法可以让 LINQ 忽略该问题并简单地提交数据吗?
Is there any way to determine which row has a conflict and which fields they occur in, and also is there a way of getting LINQ to ignore the issue and simply commit the data regardless?
此外,有人知道该异常是在行中的任何数据发生更改时发生,还是仅在 LINQ 试图更改的字段中的数据发生更改时发生?
Additionally, does anybody know whether this exception occurs when any data in the row has changed, or only when data has been changed in a field that LINQ is attempting to alter?
推荐答案
这是查看冲突位置的方法(这是一个 MSDN 示例,因此您需要大量自定义):
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);
}
}
}
让它忽略问题并提交:
db.SubmitChanges(ConflictMode.ContinueOnConflict);
这篇关于LINQ 中的数据冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!