LINQ to SQL"2个更新中的1个失败"在"SubmitChanges()"上 [英] LINQ to SQL "1 of 2 Updates failed" on "SubmitChanges()"

查看:117
本文介绍了LINQ to SQL"2个更新中的1个失败"在"SubmitChanges()"上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LINQ to SQL更新X类型的对象及其子对象Y,然后提交更改并得到此错误

I am updating an object of type X and its children Y using LINQ to SQL and then submitting changes and getting this error

示例代码

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();

在SubmitChanges()上,我得到一个异常"2个更新中的1个失败",没有其他有关发生这种情况的信息.有什么想法吗?

On SubmitChanges() I get an exception "1 of 2 Updates failed", no other information as to why that happened. Any ideas?

另外,异常类型是 ChangeConflictException

Also the exception type is ChangeConflictException

推荐答案

所以造成问题的原因是什么 -触发器

Sooo what was the cause of the problem - A trigger

我做了一个sql profiler,看到了

I did a sql profiler and saw that

当ObjY的DeletedOn属性更新时,触发器也会更新 ObjX的属性(表中的值)称为CountOfX

When ObjY's DeletedOn property got updated a trigger updated ObjX's property (value in table) called CountOfX

这导致错误,因为LINQ to SQL创建的SQL中包含旧的CountOfX值.

which led to an error as the SQL created by LINQ to SQL had the old CountOfX value in it.

发生冲突.

如果您遇到此错误-SQL事件探查器是开始调查的最佳位置

If you ever get this error - SQL profiler is the best place to start your investigation

也与该问题无关 我正在测试LINQ to SQL和ADO.net Framework,很奇怪,此错误发生在LINQ to SQL中,但未在ADO.net Framework中.但是我喜欢LINQ to SQL的延迟加载功能.等待EF退出测试版

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. But I like LINQ to SQL for its Lazy Loading. Waiting for EF to get outta beta

这篇关于LINQ to SQL"2个更新中的1个失败"在"SubmitChanges()"上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆