撤消更改在实体框架的实体 [英] Undo changes in entity framework entities

查看:422
本文介绍了撤消更改在实体框架的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个微不足道的问题,但:由于ADO.NET实体框架自动跟踪变化(产生的实体),因此保持原有的价值观,我怎么可以回滚到实体对象的更改

this might be a trivial question but: Since ADO.NET entity framework automatically tracks changes (in generated entities) and therefore keeps the original values, how can I rollback changes made to the entity objects?

我有一个表单,允许用户编辑了一套客户实体在网格视图。

I have a form which allows the user to edit a set of "Customer" entities in a grid view.

现在我有两个按钮接受和还原:如果接受被点击时,我称之为 Context.SaveChanges()和更改的对象写回到数据库中。如果还原被点击时,我想对所有对象以获得其原始属性值。会是什么code是什么?

Now I have two buttons "Accept" and "Revert": if "Accept" is clicked, I call Context.SaveChanges() and the changed objects are written back to the database. If "Revert" is clicked, I would like for all objects to get their original property values. What would be the code for that?

感谢

推荐答案

有没有恢复或在EF取消更改操作。每个实体都有 ObjectStateEntry ObjectStateManager 。国家条目包含原始值和实际值,因此您可以使用原始值覆盖当前值,但你必须手动完成为每个实体。它不会reveret改变导航属性/关系。

There is no revert or cancel changes operation in EF. Each entity has ObjectStateEntry in ObjectStateManager. State entry contains original and actual values so you can use original values to overwrite current values but you must do it manually for each entity. It will not reveret changes in navigation properties / relations.

要还原更改的处理情况和重装实体的常用方法。如果你想避免重装必须创建实体的克隆和修改新对象的上下文的克隆。如果用户取消的改变,你仍然有原来的实体。

The common way to "revert changes" is disposing context and reload entities. If you want to avoid reloading you must create clones of entities and modify those clones in new object context. If user cancel changes you will still have original entities.

这篇关于撤消更改在实体框架的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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