LINQ 中的数据冲突
- 作者: 万里大荒
- 来源: 51数据库
- 2022-12-08
问题描述
在使用 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);
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
