C# System.Data.Linq.ChangeConflictException:未找到或更改行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/805968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:56:59  来源:igfitidea点击:

System.Data.Linq.ChangeConflictException: Row not found or changed

c#.netlinq

提问by San

I am trying to delete a selected gridview row using LINQ (No LINQDataSource).

我正在尝试使用 LINQ(无 LINQDataSource)删除选定的 gridview 行。

When the selection is changed, the detailsview binding is changed also. I can add a new entry to the database, but when I added this code to a delete button inside the updatePanel, I got an exception:

更改选择时,详细信息视图绑定也会更改。我可以向数据库添加一个新条目,但是当我将此代码添加到 updatePanel 内的删除按钮时,出现异常:

try
{           
    var query = from i in db.QuestionModules 
                where i.QuestionModuleID == QuestionModuleID 
                select i;

    QuestionModule o = query.First();
    db.QuestionModules.DeleteOnSubmit(o);
    db.SubmitChanges();
}

This is the exception I get:

这是我得到的例外:

System.Data.Linq.ChangeConflictException: Row not found or changed. at
System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode
failureMode) at
System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges() 

I've had this problem for about a week, and no matter what I do, it is still there, and the record doesn't get deleted.

我遇到这个问题大约一个星期了,无论我做什么,它仍然存在,并且记录不会被删除。

Any ideas on what to do?

关于该怎么做的任何想法?

回答by Mark

I'm having the same problem, and came across this blog, which basically states that Linq-To-Sql has got a problem in it's optimistic concurrency where:

我遇到了同样的问题,并遇到了这个博客,它基本上指出 Linq-To-Sql 在它的乐观并发中遇到了问题,其中:

  1. High precision datetime fields are used. The solution is to set UpdateCheck to never for that column your DBML file
  2. GridView columns which are set to invisible are accessing a property on the data object (this second reason makes no sense, but it seems to be all the rage on that blog).
  1. 使用高精度日期时间字段。解决方案是将 UpdateCheck 设置为从不为 DBML 文件的该列
  2. 设置为不可见的 GridView 列正在访问数据对象上的属性(第二个原因没有意义,但它似乎在该博客上风靡一时)。

I haven't tried these solutions yet, but will post back here once I have.

我还没有尝试过这些解决方案,但一旦我尝试过,我会在这里发布。

回答by Mark

OK - it looks as though (in my case at least) the answer was to set allnon primary-key column's UpdateCheck property to Neverin the DBML file. Doing this immediately cured the problem of "Row not found or changed".

好的 - 看起来(至少在我的情况下)答案是在 DBML 文件中将所有非主键列的 UpdateCheck 属性设置为从不。这样做立即解决了“找不到行或未更改行”的问题。

Given the rumour that Microsoft are moth-balling Linq-To-Sql in favor of Entity Framework, one wonders whether these sorts of bugs will be fixed?

鉴于有传言称微软正在封存 Linq-To-Sql 以支持实体框架,人们想知道这些类型的错误是否会被修复?

回答by suhad

Under:

在下面:

QuestionModule o = query.First();

You have to add the following command:

您必须添加以下命令:

db.QuestionModule.Attach(o);

回答by saltyobi

Ensure that no columns contain nullvalues in the related table (i.e. the table to be updated).

确保null相关表(即要更新的表)中没有列包含值。

回答by Darwin N.

I was able to resolve this issue by executing databind() on the gridview and datasource during updatepanel postback.

我能够通过在 updatepanel 回发期间在 gridview 和数据源上执行 databind() 来解决这个问题。

    protected void UpdatePanel1_Load(object sender, EventArgs e)
    {
        GridView1.DataBind();
        LinqDataSource1.DataBind();
    }

I refresh updatepanel each time my selection index changes and it was able to resolve the conflicts.

每次我的选择索引更改时,我都会刷新 updatepanel 并且它能够解决冲突。

Hope this helps.

希望这可以帮助。

回答by Travis

Seemed to work for my situation also. I was building an in memory, never before submitted row which had several foreign key relationships to rows in other tables. The InsertOnSubmit seemed to work but a subsequent DeleteOnSubmit was giving me that row not found error. I was not populating every field in the row I submitted so I don't know if that had anything to do with it but marking all of the main table's non-primary key columns eliminated the error message.

似乎也适用于我的情况。我正在内存中构建一个从未提交过的行,该行与其他表中的行有几个外键关系。InsertOnSubmit 似乎有效,但随后的 DeleteOnSubmit 给了我该行未找到错误。我没有填充我提交的行中的每个字段,所以我不知道这是否与它有关,但是标记所有主表的非主键列消除了错误消息。

One further thought: I take it that marking a column's UpdateCheck policy as 'Never' means that it is not used as the basis for optimistic concurency checking. This would imply that two users could write over a given row with different data in any such column and the conflicts would not be detected...meaning the last user to submit the row would overwrite the values of the previous users submission. I gathered from various online reading that one partial solution to this is to use the Refresh method immediately before submission to sync up any changes. Of course with no pesimistic lock on the row there is no guarantee that the row won't still be changed between the Refresh and the Submit but in most scenarios involving large databases this would be rare.

进一步的想法:我认为将列的 UpdateCheck 策略标记为“从不”意味着它不被用作乐观并发检查的基础。这意味着两个用户可以在任何这样的列中用不同的数据覆盖给定的行,并且不会检测到冲突……这意味着最后一个提交该行的用户将覆盖先前用户提交的值。我从各种在线阅读中收集到,一个部分解决方案是在提交之前立即使用 Refresh 方法来同步任何更改。当然,在行上没有悲观锁定时,不能保证在刷新和提交之间仍然不会更改行,但在涉及大型数据库的大多数情况下,这种情况很少见。

Update: On further review I think I have discovered a scenario that may be affecting others so I thought I would share it just in case. It turns out that at least part of the trouble I have been having with SQL LINQ is related to triggers. It appears that if you submit a row using SQL LINQ and your DBA has triggers designed to write information to some columns in that row then the SQL LINQ model will by default 'Always' determine that the row has been changed since your last write of it. I was submitting partially populated rows and our DBA's triggers are filling in some columns so that when I attempted to further modify the row in our code it sensed a change conflict based on the columns populated by the trigger. I am investigating now the best way to handle this but changing these trigger populated fields to use an UpdateCheck policy of 'When Changed' or 'Never' worked for me. Hope it helps.

更新:在进一步中,我想我发现了一个可能影响其他人的场景,所以我想我会分享它以防万一。事实证明,至少我在使用 SQL LINQ 时遇到的部分问题与触发器有关。看来,如果您使用 SQL LINQ 提交一行,并且您的 DBA 具有旨在将信息写入该行中某些列的触发器,那么 SQL LINQ 模型将默认“始终”确定该行自您上次写入以来已更改. 我提交了部分填充的行,我们的 DBA 触发器正在填充一些列,因此当我尝试进一步修改代码中的行时,它根据触发器填充的列感知到更改冲突。我现在正在调查处理此问题的最佳方法,但更改这些触发器填充字段以使用“更改时”或“从不”的 UpdateCheck 策略对我有用。希望能帮助到你。

回答by Michael9000

I had similar changeconflictexception/"Row not found or changed" when updating a row. Solved it by re-adding the tabbles in the dbml.

更新行时,我遇到了类似的 changeconflicexception/"Row not found or changed"。通过在 dbml 中重新添加 tabbles 来解决它。

回答by Fayssal El Moufatich

The problem could be also simply that the DBML definition of the table is not consistent with the status of the Database definition. I just removed the DBML model and inserted it again from the database, and it worked.

问题也可能只是表的 DBML 定义与数据库定义的状态不一致。我刚刚删除了 DBML 模型并从数据库中再次插入它,并且它起作用了。

Hope this helps somebody.

希望这可以帮助某人。

回答by SemMike

You are getting this error quite possibly because one of your fields has something different in the Linq To SQL designer and in the actual database.

您收到此错误很可能是因为您的一个字段在 Linq To SQL 设计器和实际数据库中具有不同的内容。

In my case, it was because one of the fields was nullable in the database and not nullable in the designer, making it nullable in the designer as well solved the problem immediately.

就我而言,这是因为其中一个字段在数据库中可以为空,而在设计器中不可为空,因此在设计器中使其可以为空也立即解决了问题。

回答by localman

Also - if you're calling the selecting method for the linqdatasource and setting e.result manually, make sure you're including any foreignkey values as well.

此外 - 如果您正在调用 linqdatasource 的选择方法并手动设置 e.result,请确保您还包含任何外键值。

Nothing else worked for me but this.

除了这个,没有其他东西对我有用。