提交DataContext更改时发生Linq ChangeConflictException [英] Linq ChangeConflictException occurs when submitting DataContext changes
问题描述
System.Data.Linq.ChangeConflictException: 2 of X updates failed.
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at PROJECT.Controllers.HomeController.ClickProc(Int32 id, String code, String n)
这是我经常得到的.这项操作一天要执行数千次,而我大约每5秒就会收到一次此异常.据我了解,它发生在创建DataContext
和更新它之间的数据库中某些更改时.我说的对吗?
This is what I get very often. This action is done thousands of times a day, and I get this exception about once every 5 seconds. From what I understand it happens when something changes in the database in the period between creating DataContext
and updating it. Am I right?
我该如何解决?
更新
我刚刚调试了错误并发现了以下内容:
I just debugged the error and found the following:
Table name: dbo.Stats
current value: 9852039
original value: 9852038
database value: 9852039
统计信息表会不断更新.因此,如何仍然使LINQ保存更改.通过SqlDataCommand
进行经典" SQL Server访问,我从来没有遇到过这样的问题.
The Stats table is updated constantly. So how can I still make LINQ save the changes. With "classical" SQL Server access through SqlDataCommand
I never had problems like that.
推荐答案
这是由于乐观并发所致.您可以更改此行为,但在执行操作之前先了解它的作用.
This is due to optimistic concurrency. You can change this behavior but understand what it does before you do it.
https://blogs.msdn.microsoft.com/matt/2008/05/22/intro-to-linq-to-sql-optimistic-concurrency/
这篇关于提交DataContext更改时发生Linq ChangeConflictException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!