的DbContext放弃更改,恕不处理 [英] DbContext discard changes without disposing

查看:147
本文介绍了的DbContext放弃更改,恕不处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用模态窗口设置分层对象的属性的桌面客户端应用程序。由于这是一个客户端应用程序和访问的DbContext没有螺纹,我用一个长期运行的背景下,被传来传去到模态的孩子在主窗体上。

I have an desktop client application that uses modal windows to set properties for hierarchical objects. Since this is a client application and access to the DbContext is not threaded, I use a long-running context on the main Form that gets passed around to modal children.

这些模态窗口使用PropertyGrid中显示实体属性,也有取消按钮。如果任何数据被修改和取消按钮是pressed,该变化反映在父窗体(在这里我无法处置的DbContext对象)。

These modal windows use the PropertyGrid to display entity properties and also have cancel buttons. If any data is modified and the cancel button is pressed, the changes are reflected in the parent form (where I cannot dispose the DbContext object).

有没有办法放弃所做如果DbContext.SaveChanges方法尚未被称为什么变化?

Is there a way to discard any changes made if the DbContext.SaveChanges method has NOT been called?

更新:实体框架4.4版

推荐答案

在取消你可以设置当前值恢复为原始值单个实体的性质所做的更改的简单情况。

In the simple case of cancelling the changes made to properties of a single entity you can set the current values to the original values.

context.Entry(myEntity).CurrentValues.SetValues(context.Entry(myEntity).OriginalValues);
//you may also need to set back to unmodified -
//I'm unsure if EF will do this automatically
context.Entry(myEntity).State = EntityState.UnModified;

或替代重装(但会导致DB命中)

or alternatively reload (but results in db hit)

context.Entry(myEntity所).reload的();

这篇关于的DbContext放弃更改,恕不处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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