实体框架中的SaveChanges与AcceptAllChanges [英] SaveChanges vs. AcceptAllChanges in Entity Framework

查看:603
本文介绍了实体框架中的SaveChanges与AcceptAllChanges的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是, _context.SaveChanges _context.AcceptAllChanges(),是<$ C $的区别C> AcceptAllChanges()是那种从数据库重装数据或回滚(丢弃)由用户制作的时候,他并没有使用变化的SaveChanges()

What's the difference between, _context.SaveChanges and _context.AcceptAllChanges(), is the AcceptAllChanges() is sort of reloading data from Database or rolling back (discarding) changes made by the user when he didn't use SaveChanges()

推荐答案

ObjectContext.AcceptAllChanges方法 - MSDN

如果在的SaveChanges 方法被调用和的AcceptAllChangesAfterSave
体时,用户必须调用AcceptAllChanges方法。的
AcceptAllChanges方法是在一个交易
已失败的情况下有用,用户希望重试

If the SaveChanges method was called and the AcceptAllChangesAfterSave was not specified, the user must call the AcceptAllChanges method. The AcceptAllChanges method is useful in the scenario where a transaction has failed and a user wants to retry.

您可能会看到这一点: http://blogs.msdn.com/ b / alexj /存档/ 2009/01/11 /调用SaveChanges-false.aspx

You may see this: http://blogs.msdn.com/b/alexj/archive/2009/01/11/savechanges-false.aspx

如果你调用调用SaveChanges()或调用SaveChanges(真),EF简单地假设
,如果它的工作完成好,一切都没问题,所以它会
丢弃它一直跟踪的变化,并等待新的变化。

If you call SaveChanges() or SaveChanges(true),the EF simply assumes that if its work completes okay, everything is okay, so it will discard the changes it has been tracking, and wait for new changes.

但不幸的是,如果出现错误别的地方在
的交易,因为EF丢弃它跟踪的变化,我们
无法恢复。

Unfortunately though if something goes wrong somewhere else in the transaction, because the EF discarded the changes it was tracking, we can’t recover.

这是哪里的SaveChanges(假)和AcceptAllChanges()进来了。

This is where SaveChanges(false) and AcceptAllChanges() come in.

调用SaveChanges(假)告诉EF执行必要的数据库
命令,但坚持的变化,以便可以在
必要进行重播。

SaveChanges(false) tells the EF to execute the necessary database commands, but hold on to the changes, so they can be replayed if necessary.

现在如果大盘交易失败,您可以重试EF具体
位,再次调用的SaveChanges(假)。另外,您可以通过状态管理器记录哪些失败
的步行路程。

Now if the broader transaction fails you can retry the EF specific bits, with another call to SaveChanges(false). Alternatively you can walk through the state-manager to log what failed.

在更广泛的交易成功,只需调用
AcceptAllChanges(手动) ,和已被跟踪
的更改将被丢弃。

Once the broader transaction succeeds, you simply call AcceptAllChanges() manually, and the changes that were being tracked are discarded.

这篇关于实体框架中的SaveChanges与AcceptAllChanges的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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