C# 未找到或更改 Linq 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8851482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq row not found or changed
提问by Tom Gullen
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)
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.
这似乎是随机发生的。我收到了这些错误的电子邮件,报告的 URL 似乎总是对我有用,也应该对其他所有人都有效。
I can fix this error by:
我可以通过以下方式修复此错误:
- Going to my
dbmllayout - Selecting every field in the table causing conflicts
- Right click and set the property
Update ChecktoNever
- 转到我的
dbml布局 - 选择表中的每个字段导致冲突
- 右键单击并将属性设置
Update Check为Never
This seems to prevent these types of errors being thrown.
这似乎可以防止抛出这些类型的错误。
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.
但是,每当我有机会dmbl添加新表等时,记住要继续这样做是很费力的。有没有更好的方法来解决这个问题?我每天可能会收到 50-100 个这样的东西,这对我的访客不利。
采纳答案by Mike Mooney
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.
每次我看到这个错误时,都意味着在我加载记录/对象/任何东西和我试图保存它之间的时间里,数据库中的某些东西发生了变化。没有失败,那是因为我的工作单位太大了。
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.
问问自己,如果在对象的整个生命周期内对数据进行锁定事务会怎样。说您加载的任何内容都可以修改,在此期间任何人都不允许触摸它。基本上这是这里的假设。诚然,LINQ 对此稍微乐观一点,如果您可能永远不会更新数据,那么锁定行或表是没有意义的,但是您要仔细考虑这些问题。问问自己,如果您要在对象上设置严格的事务锁,什么会显着中断或减慢速度,这可能会将您指向有问题的代码。
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.
我对此的解决方案是使我的工作单元尽可能小。不要加载对象并将其用作您的工作副本并将其存储回数据库,所有这些都在一个上下文中。相反,加载对象并一步拉出您需要的信息,然后找出您需要应用的更改,然后加载/更新/保存该对象。当然它会导致更多的数据库往返,但可以更好地保证您正在使用最新的数据副本。它仍然是“后进,胜出”,这意味着如果有人在您处理数据时进行了更新,它可能会丢失,但这始终存在风险,除非您使用事务锁定记录。但是,它确实为您提供了灵活性,如果其他人正在修改同一行中不相关的字段,您可以一起操作该数据。
回答by Serame
GetTable().Attach(newEntity, originalEntity);
GetTable().Attach(newEntity, originalEntity);
If you're not using a version field to update, The above method parameterize new details and old details entities, if the original entity is changed at this point you'll still get the same error, this is up to you to ensure. This works like a charm, i've tried it.
如果您不使用版本字段进行更新,则上述方法参数化新的详细信息和旧的详细信息实体,如果此时更改原始实体,您仍然会得到相同的错误,这由您来确保。这就像一个魅力,我试过了。
回答by Tomasz Maj
I had same issue, and solved it by comparing dbml to db structure. One property wasn't set to nullable, and that was causing the issue.
我有同样的问题,并通过将 dbml 与 db 结构进行比较来解决它。一个属性未设置为可为空,这导致了问题。
So check your dbml, and nullable properties.
因此,请检查您的 dbml 和可为空的属性。
回答by jco
Sometimes it's really simple... Make sure there are no locks on the table. In my case, the SQL Server Management Studio UI held locks that I didn't know about (didn't remember) while I was debugging. Unloading it cleared those locks and things started working again.
有时真的很简单……确保桌子上没有锁。就我而言,SQL Server Management Studio UI 持有我在调试时不知道(不记得)的锁。卸载它清除了那些锁,事情又开始工作了。
回答by robertk
I was getting this error on Windows Phone 8. And it took me a while to figure it out what the problem was. Basically it was this:
我在 Windows Phone 8 上遇到了这个错误。我花了一段时间才弄清楚问题是什么。基本上是这样的:
I had a class called TrackingInformation. 1. From a server I fetched an updated version of it. 2. My TileService updated the secondary tile with it's information and (!!) updated 1 property and saved it to the local database on the phone. 3. I tried to save the first instance of the object again and here I got the error "row not found or changed".
我有一个名为 TrackingInformation 的类。1. 我从服务器获取了它的更新版本。2. 我的 TileService 用它的信息更新了辅助磁贴,(!!) 更新了 1 个属性并将其保存到手机上的本地数据库中。3. 我试图再次保存对象的第一个实例,在这里我收到错误“找不到或更改行”。
For me I just had to remove the 3rd part and it worked fine (I was already saved with the updated version). But since one property had changed it threw an exception...
对我来说,我只需要删除第三部分就可以了(我已经用更新的版本保存了)。但是由于一个属性发生了变化,它抛出了一个异常......
I can't wait until EF7 comes to Windows Phone, so much headache with the current EF on WP8.
我等不及 EF7 出现在 Windows Phone 上,WP8 上的当前 EF 太令人头疼了。
回答by Flaudre
Using SQL Profiler I tracked the SQL transactions towards my server and realized that on SubmitChanges()the transmitted UPDATEstatement contains a wrong valuein the WHEREclause. This is because I have a property that looks like this:
使用 SQL Profiler 我跟踪到我的服务器的 SQL 事务,并意识到在SubmitChanges()传输的UPDATE语句中包含错误的WHERE子句值。这是因为我有一个如下所示的属性:
class Position{
...
[Column]
public int Pieces{
get{
this.pieces = this.Transformers.Count;
return this.pieces;
}
set{ this.pieces = value; }
}
...
}
Since Transformersis a List<Transformers>element that represents a list of One-To-Many relations to another Transformerstable, I manually have to fill the List with each Transformerthat contains a foreign key to my Positionobject. If, for some reason, I don't do that, then I will get the mentionned error because the Piecesproperty is biased from the original value.
由于Transformers是一个List<Transformers>元素,它表示与另一个Transformers表的一对多关系列表,我必须手动填充列表,其中每个Transformer都包含我的Position对象的外键。如果出于某种原因,我不这样做,那么我会得到提到的错误,因为该Pieces属性与原始值有偏差。
Thus, the SQL statement on SubmitChanges()will search for an entry WHERE Pieces = somevaluebut the actual value of Piecesin the entry is anothervalue. So basically LINQ will think, that you changed something in the db meanwhile, because the db will return no value to this request.
因此,对SQL语句SubmitChanges()将搜索条目WHERE Pieces = somevalue,但实际值Pieces中的项是anothervalue。所以基本上 LINQ 会认为,您同时更改了 db 中的某些内容,因为 db 不会为此请求返回任何值。
As stated, one solution is:
如上所述,一种解决方案是:
[Column(UpdateCheck=UpdateCheck.Never)]
public int Pieces
{
get {
this.pieces= this.Transformers.Count;
return this.pieces;
}
set { this.pieces= value; }
}
Of course this can also be easily solved by setting up a simple getter function like
当然,这也可以通过设置一个简单的 getter 函数来轻松解决,例如
getPieces(){
return this.Transformers.Count();
}
and an "unbiased" property
和“无偏见”的财产
class Position{
...
[Column]
public int Pieces{
get{ return this.pieces; }
set{ this.pieces = value; }
}
...
}
I hope this can be of help to somebody. I post this also because if somebody else had before, it'd have saved me hours of my life.
我希望这可以对某人有所帮助。我发布这个也是因为如果之前有人这样做过,它会为我节省数小时的生命。
回答by Jacob
This can also happen if a database trigger changes any column in a row you are updating - even if you are not updating that particular column.
如果数据库触发器更改了您正在更新的行中的任何列,即使您没有更新该特定列,也会发生这种情况。
You will have to double check that any of the tables affected in your unit of work don't have triggers on them. If you are using update triggers, then you will probably want to make sure the updated columns are NeverCheck.
您必须仔细检查工作单元中受影响的任何表是否没有触发器。如果您使用更新触发器,那么您可能需要确保更新的列是NeverCheck。
回答by Ivan Ivanov
updating linq to sql schema in designer resolve problem in my case
在设计器中将 linq 更新为 sql 架构解决了我的问题
回答by developer learn999
I have faced the same issue.The proble occures where you update a row.
我遇到了同样的问题。问题发生在你更新一行的地方。
For example i have a table with columns:
例如,我有一个带有列的表:
id,firstname,lastname,age
and I want, to update the age using the dbml. In the profiler i see
update age=@newagewhere id=@idand firstname=@firstnameand lastame=@lastname.
我想使用 dbml 更新年龄。在探查器中,我看到更新age=@newagewhereid=@id和firstname=@firstnameand lastame=@lastname。
But between reading the row and updating, some other process changed the first name or last name value. The UPDATE fails because the WHEREclause has different value for one of the columns, and the update return @@rowcount==0to the dbml, which troughs the error you getting.
但是在读取行和更新之间,其他一些进程更改了名字或姓氏值。UPDATE 失败是因为该WHERE子句对于其中一列具有不同的值,并且更新返回@@rowcount==0到 dbml,这会导致您遇到的错误。

