問題描述
在使用 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 中的數據沖突的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!