简体   繁体   English

LINQ to SQL“ SubmitChanges()”上的“ 2个更新中的1个失败”

[英]LINQ to SQL “1 of 2 Updates failed” on “SubmitChanges()”

I am updating an object of type X and its children Y using LINQ to SQL and then submitting changes and getting this error 我正在使用LINQ to SQL更新X类型的对象及其子对象Y,然后提交更改并得到此错误

Example Code 范例程式码

X objX = _context.X.ToList().Where(x => x.DeletedOn == null).First();
objX.DeletedOn = DateTime.Now;

EntitySet<Y> objYs = objX.Ys;
Y objY = objYs[0];
objY.DeletedOn = DateTime.Now;

_context.SubmitChanges();

On SubmitChanges() I get an exception "1 of 2 Updates failed", no other information as to why that happened. 在SubmitChanges()上,我得到一个异常“ 2个更新中的1个失败”,没有其他信息说明发生这种情况的原因。 Any ideas? 有任何想法吗?

Also the exception type is ChangeConflictException 另外,异常类型是ChangeConflictException

Sooo what was the cause of the problem - A trigger 找出问题的原因是什么-触发

I did a sql profiler and saw that 我做了一个SQL事件探查器,看到了

When ObjY's DeletedOn property got updated a trigger updated ObjX's property (value in table) called CountOfX 当ObjY的DeletedOn属性更新时,触发器更新了ObjX的属性(表中的值),称为CountOfX

which led to an error as the SQL created by LINQ to SQL had the old CountOfX value in it. 这导致错误,因为LINQ to SQL创建的SQL中包含旧的CountOfX值。

Hence the conflict. 因此发生了冲突。

If you ever get this error - SQL profiler is the best place to start your investigation 如果您遇到此错误-SQL事件探查器是开始调查的最佳位置

ALSO NOT RELATED TO THE QUESTION I am testing LINQ to SQL and ADO.net Framework, weirdly this error happened in LINQ to SQL but not in ADO.net framework. 在LINQ 也不涉及这个问题我测试LINQ to SQL和ADO.net框架,正巧SQL但不是在ADO.net框架古怪此错误。 But I like LINQ to SQL for its Lazy Loading. 但是我喜欢LINQ to SQL的延迟加载功能。 Waiting for EF to get outta beta 等待EF退出测试版

I'm not sure what the cause of the error may be exactly, but there seem to be a number of problems with the example you've provided. 我不确定导致错误的原因到底是什么,但是您提供的示例似乎存在许多问题。

  • Using ToList() before the Where() method would cause your context to read the entire table from the DB into memory, convert it to an array; 在Where()方法之前使用ToList()会导致您的上下文将整个表从DB读取到内存中,并将其转换为数组; and then in the same line you immediately call Where which will discard the rows you've loaded, but don't need. 然后在同一行中立即调用Where,它将丢弃您已加载但不需要的行。 Why not just: 为什么不只是:

    _context.X.Where(... _context.X.Where(...

  • The Where method will return multiple items, but the second line in the example doesn't appear to be iterating through each item individually. Where方法将返回多个项目,但是示例中的第二行似乎并未分别遍历每个项目。 It appears to be setting the DeletedOn property for the collection itself, but the collection wouldn't have such a property. 它似乎在为集合本身设置DeletedOn属性,但是该集合没有这样的属性。 It should fail right there. 它应该在那里失败。

  • You are using DateTime.Now twice in the code. 您在代码中两次使用DateTime.Now。 Not a problem, except that this will produce ever so slightly different date values each time it is called. 没问题,只是每次调用都会产生稍有不同的日期值。 You should call DateTime.Now once and assign the result to a variable so that everything you use it on gets identical values. 您应该调用一次DateTime.Now并将结果分配给一个变量,以便您使用它的所有内容都具有相同的值。

  • At the point where you have "Y objY = objYs[0]" it will fail if there are no items in the Y collection for any given X. You'd get an index out of bounds exception on the array. 在您拥有“ Y objY = objYs [0]”的地方,如果任何给定X的Y集合中没有任何项目,它将失败。您将在数组上获得索引超出范围的异常。

So given this example, I'm not sure if anyone could speculate as to why code modeled after this example might be breaking. 因此,在给出此示例的情况下,我不确定是否有人可以推测为什么在此示例之后建模的代码可能会中断。

In LINQ2SQL Data Context diagram select the Entity and the field where the count is stored. 在LINQ2SQL数据上下文图中,选择实体和存储计数的字段。 (A denormalized figure) (非正规化的数字)

Now set the UpdateCheck = Never. 现在设置UpdateCheck = Never。

I had this kind of issue. 我有这种问题。 I was debugging running single lines at a time. 我一次调试一行。 It turned out another process was modifying this record. 事实证明,另一个过程正在修改此记录。

My manual debugging process was slowing down the normal speed of the function. 我的手动调试过程降低了该功能的正常速度。 When I ran it all the way to a line after the SubmitChanges method, it succeeded. 当我将它一直运行到SubmitChanges方法之后的一行时,它成功了。

My scenario would be less common, but the nature of this error relates to the record becoming superceded by another function/process. 我的情况不太常见,但是此错误的性质与记录被另一个功能/进程取代有关。 In my case it was another process. 就我而言,这是另一个过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM