EF CodeFirst:使用承诺控制删除 [英] EF CodeFirst: Delete with commitment control

查看:84
本文介绍了EF CodeFirst:使用承诺控制删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在EF CodeFirst中执行以下操作



检索记录,删除它。

执行其他任务。 />
如果其他任务成功完成,则需要提交删除。

否则,需要回滚删除,就像以前从未删除过记录一样。



有没有人有一个样本C#codefirst来执行这个承诺控制。

我在网上搜索并找不到任何样本。



事先谢谢。

I need to perform the following, in EF CodeFirst

Retrieve a record, Delete it.
Do other tasks.
If other tasks are done successfully, the deletion needs to be commited.
Otherwise, the deletion needs to be rolled back as if the record has never been deleted before.

Does anyone has a sample C# codefirst to perform this commitment control.
I search the web and can not find any sample of it.

Thanks beforehand.

推荐答案

这没什么特别的。当您从EF中删除记录时,它只是被删除而已被删除。在DbContext上调用SaveChanges之前,不会从数据库中删除它。



所以,如果你不想删除记录,只需将其状态改回Unchanged:

There''s nothing special about this. When you Remove the record from EF it''s just TAGGED to be deleted. It won''t be deleted from the database until you call SaveChanges on your DbContext.

So, if you don''t want the record to be removed, just change its state back to Unchanged:
myDbContext.Entry(myEntityObject).State = EntityState.Unchanged





但是,你有一个你的逻辑中更大的问题。你为什么甚至删除记录,除非你确认需要删除它?



But, you''ve got a bigger problem in your logic. Why are you even "deleting" the record unless you have confirmed that it needs to be deleted?


这篇关于EF CodeFirst:使用承诺控制删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆