LINQ 上下文 SubmitChanges
关于 SubmitChanges 顺序(插入、更新、删除),有没有办法更改该顺序?我需要先执行删除,然后执行任何更新,然后执行任何新插入。我有一个数据网格,用户可以在其中执行所有添加、更改和更新并提交。由于每个网格行必须在其中选择一个唯一的项目(通过下拉列表),因此用户可以删除该行,然后尝试在新行中使用已删除的下拉项目,然后尝试更新所有更改并拥有更新失败(因为用户想要删除的项目实际上仍然存在于数据库中,因为提交首先执行插入)。是否有一个设置可以控制自动更新顺序,或者我必须自己手动进行更新?
Regarding the SubmitChanges order (Insert, Update, Delete), is there a way to change that order? I need to have the Deletes executed first, any updates, then any new inserts. I have a datagrid where the user can do all add, changes and updates and submit. Since each grid row must have a unique item chosen in it (via a dropdown), it's possible for the the user to delete the row, then try to utilize the deleted dropdown item in a new row, then try to update all changes and have the update fail (since the item the user wants to delete actually still exists in the database because the submit is doing the inserts first). Is there a setting where I can control the automatic update order or do I have to manually do the updates myself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有尝试过,但你可以考虑以下方法。首先,使用
DataContext.ChangeSet
。然后,在DataContext
的新实例上运行调用Table.DeleteOnSubmit
的ChangeSet.Deletes
。重复冲洗ChangeSet.Updates
和ChangeSet.Inserts
。祝你好运。
I have not tried this, but you could consider the following. First, get the
ChangeSet
usingDataContext.ChangeSet
. Then, run through theChangeSet.Deletes
callingTable<T>.DeleteOnSubmit
on a new instance of yourDataContext
. Rinse repeat for theChangeSet.Updates
and theChangeSet.Inserts
.Good luck.
我不相信这是可能做到的。您必须按照您想要的顺序处理更改,并在每次插入、更新或删除后调用 SubmitChanges()。如果您希望整个事务都在事务范围内,请使用 TransactionScope 对象。
I don't believe it's possible to do this. You would have to process the changes in the order you want, and call SubmitChanges() after each insert, update or delete. If you want the whole thing within the scope of a transaction, use the TransactionScope object.