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

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

问题描述

我有一个桌面客户端应用程序使用模态窗口来设置分层对象的属性。由于这是一个客户端应用程序,并且对DbContext的访问不是线程化的,所以我使用主窗体上长时间运行的上下文传递给模态子项。



这些模态窗口使用PropertyGrid显示实体属性,并具有取消按钮。如果任何数据被修改并且取消按钮被按下,则更改将反映在父表单中(我不能处理 DbContext对象)。



如果未调用 DbContext.SaveChanges()方法,是否有办法放弃所做的更改?



更新:实体框架版本4.4。

解决方案

它在一个交易中?

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

//做某事
context.SaveChanges();
//执行其他操作
context.SaveChanges();

scope.Complete();
}


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.

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).

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

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天全站免登陆