LINQ的行未找到或改变 [英] Linq row not found or changed

查看:183
本文介绍了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.

我可以修正这个错误:


  • 将我的 DBML 布局

  • 选择表中的各个领域造成冲突

  • 右键单击并设置属性更新检查从不

  • Going to my dbml layout
  • Selecting every field in the table causing conflicts
  • Right click and set the property Update Check to Never

这似乎是prevent被抛出这些类型的错误。

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是它多了几分乐观,锁定该行或表,如果你可能永远不会更新数据,但是你想通过这些问题没有任何意义。问问自己,什么会破裂或如果你把严格的事务锁在你的对象显著放缓,这可能会指向你得罪code。

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.

我对这个解决方案是让我的工作单位尽可能小。不要加载对象,并使用它作为你的工作副本,并将其存储到数据库中,都在一个范围内。相反,加载对象,并拉出你在一个步骤需要的信息,然后找出你需要应用更改,然后加载/更新/保存对象。当然它会导致更多的往返到数据库中,但给你一个更好的保证你的数据的最新副本的工作。它仍然是迟来的胜利,这意味着如果有人做了更新,而你正在与它可能会丢失数据的工作,但是这始终是一个风险,除非你锁定了交易记录。但是,它确实给你买,如果别人在同一行正在修改不相关的领域,你都可以对数据进行操作一起flexiblity。

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天全站免登陆