DbContext 丢弃更改而不处理 [英] DbContext discard changes without disposing

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

问题描述

我有一个桌面客户端应用程序,它使用模式窗口来设置分层对象的属性.由于这是一个客户端应用程序并且对 DbContext 的访问不是线程化的,因此我在主 Form 上使用了一个长时间运行的上下文,该上下文被传递给模态子级.

I have a 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 来显示实体属性,并且还有取消按钮.如果修改了任何数据并按下了取消按钮,则更改会反映在父窗体中(我无法在其中处理 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.

UPDATE: Entity Framework Version 4.4.

推荐答案

如何将其包装在事务中?

How about wrapping it in a transaction?

    using(var scope = new TransactionScope(TransactionScopeOption.Required,
        new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })){

        // Do something 
        context.SaveChanges();
        // Do something else
        context.SaveChanges();

        scope.Complete();
}

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

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