未找到或更改 Linq 行 [英] Linq row not found or changed

查看:47
本文介绍了未找到或更改 Linq 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Error Message: Row not found or changed.
Stack Trace:
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) 
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) 

这似乎是随机发生的.我收到了这些错误的电子邮件,报告的 URL 似乎总是对我有用,并且应该对其他所有人也有效.

This occurs seemingly randomly. I get emailed these errors, and the URL reported always seems to work for me and should be functioning for everyone else as well.

我可以通过以下方式修复此错误:

I can fix this error by:

  • 转到我的 dbml 布局
  • 选择表中的每个字段导致冲突
  • 右键单击并将属性Update Check 设置为Never
  • Going to my dbml layout
  • Selecting every field in the table causing conflicts
  • Right click and set the property Update Check to Never

这似乎可以防止抛出这些类型的错误.

This seems to prevent these types of errors being thrown.

但是,每当我有机会对 dmbl 进行修改、添加新表等时,都很难记住要继续这样做.有没有更好的方法来解决这个问题?我每天可能收到 50-100 个这样的东西,这对我的访客来说是不利的.

However, this is laborious to remember to keep doing whenever I make a chance to the dmbl, add new tables, etc. Is there a better way to solve this problem? I'm getting maybe 50-100 of these a day which is bad for my visitors.

推荐答案

每次我看到这个错误,这意味着在我加载记录/对象/任何时间和我加载记录/对象/任何时间之间数据库中的某些内容发生了变化试图保存它.没有失败,那是因为我的工作单位太大了.

Every time I've seen this error, it means that something changed in the database between the time that I loaded the record/object/whatever and when I was trying to save it. Without fail, it was because my unit of work was too big.

我不知道您的应用程序的确切性质,但我假设您正在创建数据上下文、加载记录或记录列表、对其执行一些操作、消耗一些时间和处理器周期,以及然后最后尝试将修改后的数据保存回数据库.甚至可能加载记录/对象的实例并将其存储在类变量中一段时间​​,然后在页面加载或线程或任何试图保存更改的任何内容的末尾.问题是因为 LINQ 存储了它的副本,这就是它想要更新的副本.如果在此期间底层数据发生变化,它会变得疯狂.

I don't know the exact nature of your application, but I'm assuming you are creating a data context, loading a record or list of records, performing some operations on it, chewing up some time and processor cycles, and then at the end trying to save the modified data back to the database. Maybe even loading an instance of a record/object and storing it in a class variable for a while, and then at the end of a page load or thread or whatever trying to save back anything that changed. The problem is that because LINQ stored a copy of that, that is the copy it wants to update. If the underlying data changes in the meantime, it gets mad.

问问自己,如果在对象的整个生命周期内对数据进行锁定事务会怎样.说您加载的任何内容都可以修改,在此期间其他人都不允许触摸它.基本上这就是这里的假设.诚然,LINQ 对此稍微乐观一点,如果您可能永远不会更新数据,那么锁定行或表是没有意义的,但是您要仔细考虑这些问题.问问自己,如果您在对象上设置严格的事务锁,什么会严重破坏或减慢速度,这可能会将您指向有问题的代码.

Ask yourself this, what if you put a locking transaction on your data for the whole lifetime of your objects. Say anything you loaded any may modify, nobody else would be allowed to touch it during that time. Basically that is the assumption here. Granted, LINQ is a little more optimistic about it, no sense in locking the row or table if you may never be updating the data, but you think through those problems. Ask yourself what would break or slow down significantly if you were to put strict transactional locks on your objects, and that will probably point you to the offending code.

我对此的解决方案是使我的工作单元尽可能小.不要加载对象并将其用作您的工作副本并将其存储回数据库,所有这些都在一个上下文中.相反,加载对象并一步拉出您需要的信息,然后找出您需要应用的更改,然后加载/更新/保存该对象.当然它会导致更多的数据库往返,但可以更好地保证您正在使用最新的数据副本.它仍然是后进,胜出",这意味着如果有人在您处理数据时进行了更新,它可能会丢失,但这始终存在风险,除非您使用事务锁定记录.但是,它确实为您提供了灵活性,如果其他人正在修改同一行中不相关的字段,您可以一起操作该数据.

My solution to this is to keep my unit of work as small as possible. Don't load the object and use that as your working copy and store it back to the database, all in one context. Instead, load the object and pull out the info you need in one step, then figure out the changes you need to apply, and then load/update/save the object. Sure it causes more round trips to the database, but gives you a better assurance that you are working with the latest copy of data. It will still be "last in, wins", meaning that if someone made an update while you were working with the data it may be lost, but that is always a risk unless you lock the record with a transaction. However, it does buy you the flexiblity that if someone else is modifying unrelated fields in the same row, you both can operate on that data together.

这篇关于未找到或更改 Linq 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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