[英]Exception Updating All records in DB in Entity Framework
I have this weird issue with Entity Framework with the code snippet below我在实体框架中有这个奇怪的问题,下面的代码片段
var userWorkItems = (from uw in context.UserWorkItems
join u in context.DNN_Users on uw.UserID equals u.UserID
where u.Username.StartsWith(fromUserName)
select uw).ToList();
if (userWorkItems != null)
{
for (int i = 0; i < userWorkItems.Count; i++)
{
userWorkItems[i].UserID = toUserID.Value;
}
context.SubmitChanges();
}
The whole table gets updated with the Userid
of the fromUserName
when an exception occurs.发生异常时,整个表都会使用fromUserName
的Userid
进行更新。
What kind of exception leads to this kind of weird behavior, though a try catch has been added now to the context.SubmitChanges();
什么样的异常会导致这种奇怪的行为,尽管现在已经在context.SubmitChanges();
添加了一个 try catch context.SubmitChanges();
Or are we making the wrong conclusion?还是我们得出了错误的结论?
Where does the exception occur and what is the exception?异常发生在哪里,异常是什么? I believe your issue might be fromUserName
.我相信您的问题可能是fromUserName
。 If it was an empty string, it would return every single UserWorkItem
.如果它是一个空字符串,它将返回每个UserWorkItem
。 If an error is thrown and you don't want it to do a partial save on the context, wrap it in a TransactionScope
like so.如果抛出错误并且您不希望它对上下文进行部分保存,请将其包装在TransactionScope
如下所示。
using(var scope = new TransactionScope()){
//Do whatever
scope.Complete();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.