LINK

With the Entity Framework most of the time SaveChanges() is sufficient. This creates a transaction, or enlists in any ambient transaction, and does all the necessary work in that transaction.

Sometimes though the SaveChanges(false) + AcceptAllChanges() pairing is useful.

The most useful place for this is in situations where you want to do a distributed transaction across two different Contexts.

I.e. something like this (bad):

using (TransactionScope scope = new TransactionScope())
{
//Do something with context1
//Do something with context2 //Save and discard changes
context1.SaveChanges(); //Save and discard changes
context2.SaveChanges(); //if we get here things are looking good.
scope.Complete();
}

If context1.SaveChanges() succeeds but context2.SaveChanges() fails the whole distributed transaction is aborted. But unfortunately the Entity Framework has already discarded the changes on context1, so you can't replay or effectively log the failure.

But if you change your code to look like this:

using (TransactionScope scope = new TransactionScope())
{
//Do something with context1
//Do something with context2 //Save Changes but don't discard yet
context1.SaveChanges(false); //Save Changes but don't discard yet
context2.SaveChanges(false); //if we get here things are looking good.
scope.Complete();
context1.AcceptAllChanges();
context2.AcceptAllChanges(); }

While the call to SaveChanges(false) sends the necessary commands to the database, the context itself is not changed, so you can do it again if necessary, or you can interrogate the ObjectStateManager if you want.

This means if the transaction actually aborts you can compensate, by either re-trying or logging state of each contexts ObjectStateManager somewhere.

See my blog post for more.

Entity Framework - Using Transactions or SaveChanges(false) and AcceptAllChanges()?的更多相关文章

  1. (转载)Why you shouldn't use Entity Framework with Transactions

    Why you shouldn't use Entity Framework with Transactions EntityFramework This is a .net ORM Mapper F ...

  2. entity framework无法写入数据库.SaveChanges()失败

    参考https://stackoverflow.com/questions/26745184/ef-cant-savechanges-to-db/28256645 https://www.codepr ...

  3. Why you shouldn't use Entity Framework with Transactions

    Links EntityFramework This is a .net ORM Mapper Framework from Microsoft to help you talking with yo ...

  4. Entity Framework中AutoDetectChangesEnabled為false時更新DB方法

    Entity Framework初始化時執行: Configuration.AutoDetectChangesEnabled = false; 會將數據庫變為NotTrack模式,也就是不會自動同步对 ...

  5. 《Entity Framework 6 Recipes》中文翻译系列 (41) ------ 第七章 使用对象服务之标识关系中使用依赖实体与异步查询保存

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 7-7  标识关系中使用依赖实体 问题 你想在标识关系中插入,更新和删除一个依赖实体 ...

  6. Code First :使用Entity. Framework编程(6) ----转发 收藏

    Chapter6 Controlling Database Location,Creation Process, and Seed Data 第6章 控制数据库位置,创建过程和种子数据 In prev ...

  7. 了解Entity Framework中事务处理

    Entity Framework 6以前,框架本身并没有提供显式的事务处理方案,在EF6中提供了事务处理的API. 所有版本的EF,只要你调用SaveChanges方法进行插入.修改或删除,EF框架会 ...

  8. Entity Framework 的事务

    一个db.SaveChanges()相当于一个事务,多个db.SaveChanges()保证操作完整性则需要使用事务 在Entity Framework 中使用事务,事务只会对数据库操作进行回滚,不会 ...

  9. ADO.NET Entity Framework学习笔记(3)ObjectContext

    ADO.NET Entity Framework学习笔记(3)ObjectContext对象[转]   说明 ObjectContext提供了管理数据的功能 Context操作数据 AddObject ...

随机推荐

  1. css li 不换行(布局,内容)

    参考这里 ------ 不换行的策略: 不换行原理: ul 和 li 默认都是 display:block; 的标签, 可以通过2种方式实现 li 的 不换行显示: * 将 li 设为 display ...

  2. Fiddler+Jmeter+断言详细教程

    一.Fiddler抓包工具的配置和使用 在编写网关自动化脚本之前,得先学会如何抓包,这里以Fiddler为例.会抓包的同学可以跳过这一步,当然看看也是没坏处的-- 局域网络配置 将要进行抓包的手机与电 ...

  3. 强制重启N种方法

    强制重启N种方法 2015-12-24 17:19 146人阅读 评论(0) 收藏 举报 本文章已收录于:   分类: 驱动开发学习(458) 作者同类文章X 1. 无意中看到一种通过控制92H端口b ...

  4. python第一天

    python 解释器执行代码有两种 一种在解释器: win+R==>cmd 打开终端进行 输入python 加 路径 另一种在文件里写完再到解释器执行:win+R==>cmd 打开终端进行 ...

  5. JVM学习笔记:JVM的体系结构与JVM的生命周期

    1 JVM在java平台中的位置 1.1 Java平台组成 Java平台主要由Java虚拟机和Java API这两部分组成.参考Oracle官网. 1.2 java平台结构图 JDK1.2开始,迫于J ...

  6. php发送http put/patch/delete请求

    今天学RESTful API的编写,发现不知道怎么发送HTTP PUT/PATCH/DELETE请求,还是要学习一个. 使用curl_opt函数来发送各式各样的http请求动作,不仅限于get和pos ...

  7. springMVC-数据绑定流程

    1.Spring MVC主框架将ServletRequest对象以及目标方法的入参实例传递给WebDataBinderFactory实例,以创建DataBinder(数据绑定器)实例对象 2.Data ...

  8. 【codevs1034】 家园

    http://codevs.cn/problem/1034/ (题目链接) 题意 给出一张n个点的图,有m架飞船按照固定的航班运行,没单位时间移动一次,并且没收航班都有自己的容纳量.问从0号点将K个人 ...

  9. 洛谷P1629 邮递员送信

    题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要一定的时间.这个邮递员每 ...

  10. ecshop /category.php SQL Injection Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Relevant Link: http://sebug.net/vuld ...