Entity Framework: SaveChanges()和AcceptAllChanges()方法

SaveChanges()方法用于将对象中的数据保存至数据库并将对象追踪状态复位。在交易操作中,如果保存数据产生异常,该方法将取消交易。
AcceptAllChanges()方法接受数据保存结果并修改对象状态。对于增加和修改的对象,状态改变为Unchanged,对于删除的对象状态改变为Detached。
如果调用了SaveChanges()方法而未使用AcceptAllChangesAfterSave,则必须调用AcceptAllChanges()方法。在交易操作中,如果允许用户在交易失败后重试,AcceptAllChanges()方法是非常有用的。
 

Call AcceptAllChanges() to accepts the changes on all associated entries in the ObjectStateManager so their resultant state is either unchanged or detached.

This method iterates all the ObjectStateEntry objects within the ObjectStateManager that are Added or Modified, and then sets the state of the entry to Unchanged. The Deleted items become detached.

SaveChanges() persists all updates to the data source and resets change tracking in the object context. It operates within a transaction. SaveChanges will roll back that transaction and throw an exception if any of the dirty ObjectStateEntry objects cannot be persisted.

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.

Reference: http://msdn.microsoft.com

转载于:https://www.cnblogs.com/sungang3225/p/3644402.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值