DbContext.SaveChanges - 不要AcceptChangesDuringSave [英] DbContext.SaveChanges - Don't AcceptChangesDuringSave

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

问题描述

您好,

使用DbContext,我希望在保存期间不接受更改(使用ObjectContext,我们可以将布尔值传递给Save方法)。 />
是否可以使用DbContext执行此操作?我正在进行交易,如果交易没有提交,我不希望接受更改。



诚然,

Kakone。

With the DbContext, I would like to not accept changes during save (with the ObjectContext, we can pass a boolean to the Save method).
Is it possible to do this with the DbContext ? I'm in a transaction, I wouldn't like that the changes be accepted if the transaction is not committed.

Cordially,
Kakone.

推荐答案

默认情况下,此功能未在DbContext上公开,但你可以通过在派生的上下文中添加以下方法来公开它:

This functionality isn't exposed on DbContext by default but you can expose it by adding the following method to your derived context:

public int SaveChanges(SaveOptions options)
{
  return ((IObjectContextAdapter)this).ObjectContext.SaveChanges(options);
}

 

我们选择不在为了使DbContext上的概念计数尽可能低。诸如此类的更高级功能需要降低到地方的基础上下文。

We chose not to expose this in order to keep the concept count on DbContext as low as possible. More advanced functionality such as this requires dropping down to the underlying context in places.

~Rowan


这篇关于DbContext.SaveChanges - 不要AcceptChangesDuringSave的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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