如何在一次调用 SubmitChanges() 中对实体进行更新和删除
var dc = new DataContext();
var myItem = dc.Items.Single(x => x.Id == 12);
myItem.LastModifiedBy = "me";
myItem.LastModifiedDateTime = DateTime.Now;
// dc.SubmitChanges(); I don't want to do this!
dc.DeleteOnSubmit(myItem);
dc.SubmitChanges(); // does not perform the update
我对 myItem 进行了更改,并且出于审计目的,我希望对数据库进行该更改。我不想调用 SubmitChanges() 两次。我希望这发生在对 SubmitChanges() 的一次调用中,以利用隐式事务。如何告诉 DataContext 执行更新和删除操作?该代码仅执行删除操作。我知道更新没有被调用,因为数据库表的更新触发器没有触发。 DataContext 可能认为既然它被标记为删除,那么更新就没有意义了。
有什么建议吗?理想情况下,这意味着不必使用存储过程。另外,由于各种原因,我无法使用 TransactionScope。
var dc = new DataContext();
var myItem = dc.Items.Single(x => x.Id == 12);
myItem.LastModifiedBy = "me";
myItem.LastModifiedDateTime = DateTime.Now;
// dc.SubmitChanges(); I don't want to do this!
dc.DeleteOnSubmit(myItem);
dc.SubmitChanges(); // does not perform the update
I made a change to myItem, and I want that change to be made to the database, for auditing purposes. I don't want to call SubmitChanges() twice. I want this to happen in a single call to SubmitChanges(), to take advantage of the implicit transaction. How can I tell the DataContext to do both the update and the delete? The code only does the delete. I know the update is not called because the database table's update trigger doesn't fire. The DataContext probably thinks that since it's marked for deletion, there's no point bothering with the update.
Any suggestions? Ideally, it would mean not having to use a stored procedure. Also, I am not able, for various reasons, to use TransactionScope.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎此问题的最佳解决方案是更改 DELETE 触发器以插入正确的
DateTime
,并完全跳过额外的不必要的更新。Seems like the best solution to this problem would be to change the DELETE trigger to insert the proper
DateTime
, and skip doing the extra needless update altogether.