DbContext丢弃更改而不处置 [英] DbContext discard changes without disposing
问题描述
这些模态窗口使用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屋!