Linq:在 TransactionScope 中删除和插入相同的主键值
我想在一个事务中用新记录替换数据库中的现有记录。使用TransactionScope,
using ( var scope = new TransactionScope())
{
db.Tasks.DeleteAllOnSubmit(oldTasks);
db.Tasks.SubmitChanges();
db.Tasks.InsertAllOnSubmit(newTasks);
db.Tasks.SubmitChanges();
scope.Complete();
}
我的程序抛出了
System.InvalidOperationException: Cannot add an entity that already exists.
一些尝试和错误之后,我发现罪魁祸首在于删除和插入之间没有任何其他执行指令。如果我在第一个 SubmitChanges() 和 InsertAllOnSubmit() 之间插入其他代码,一切正常。谁能解释为什么会发生这种情况?这是非常令人担忧的。
我尝试了另一种方法来更新对象:
IEnumerable<Task> tasks = ( ... some long query that involves multi tables )
.AsEnumerable()
.Select( i =>
{
i.Task.Duration += i.LastLegDuration;
return i.Task;
}
db.SubmitChanges();
这也不起作用。 db 没有发现任务的任何更改。
编辑:
此行为似乎与事务没有任何关系。最后我采用了效率极低的Update:
newTasks.ForEach( t =>
{
Task attached = db.Tasks.Single( i => ... use primary id to look up ... );
attached.Duration = ...;
... more updates, Property by Property ...
}
db.SubmitChanges();
I want to replace existing records in the DB with new records in one transaction. Using TransactionScope, I have
using ( var scope = new TransactionScope())
{
db.Tasks.DeleteAllOnSubmit(oldTasks);
db.Tasks.SubmitChanges();
db.Tasks.InsertAllOnSubmit(newTasks);
db.Tasks.SubmitChanges();
scope.Complete();
}
My program threw
System.InvalidOperationException: Cannot add an entity that already exists.
After some trial and error, I found the culprit lies in the the fact that there isn't any other execution instructions between the delete and the insert. If I insert other code between the first SubmitChanges() and InsertAllOnSubmit(), everything works fine. Can anyone explain why is this happening? It is very concerning.
I tried another one to update the objects:
IEnumerable<Task> tasks = ( ... some long query that involves multi tables )
.AsEnumerable()
.Select( i =>
{
i.Task.Duration += i.LastLegDuration;
return i.Task;
}
db.SubmitChanges();
This didn't work neither. db didn't pick up any changes to Tasks.
EDIT:
This behavior doesn't seem to have anything to do with Transactions. At the end, I adopted the grossly inefficient Update:
newTasks.ForEach( t =>
{
Task attached = db.Tasks.Single( i => ... use primary id to look up ... );
attached.Duration = ...;
... more updates, Property by Property ...
}
db.SubmitChanges();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试通过选择要更新的 Id 列表并检查该列表是否包含每一项来一次性更新多行,而不是插入和删除或进行多个查询。
另外,请确保将事务标记为完成,以向事务管理器指示所有资源的状态是一致的,并且可以提交事务。
根据您的情况,如果您的表非常大并且要更新的项目数相当小,您可以反转搜索,以利用索引。如果是这种情况,您现有的更新查询应该可以正常工作,所以我怀疑您是否需要反转它。
Instead of inserting and deleting or making multiple queries, you can try to update multiple rows in one pass by selecting a list of Id's to update and checking if the list contains each item.
Also, make sure you mark your transaction as complete to indicate to transaction manager that the state across all resources is consistent, and the transaction can be committed.
Depending on your scenario, you can invert the search in the case that your table is extremely large and the number of items to update is reasonably small, to leverage indexing. Your existing update query should work fine if this is the case, so I doubt you'll need to invert it.
我在 LinqToSql 中遇到了同样的问题,我不认为它与事务有关,而是与会话/上下文如何合并更改有关。我这样说是因为我通过绕过 linqtosql 进行删除并使用一些原始 sql 来解决问题。我知道这很丑,但它有效,而且都在事务范围内。
I had same problem in LinqToSql and I don't think its to do with the transaction, but with how the session/context is coalescing changes. I say this because I fixed the problem by bypassing linqtosql for the delete and using some raw sql to do it. Ugly I know, but it worked, and all inside a transaction scope.