Entity Framework - Using Transactions or SaveChanges(false) and AcceptAllChanges()?

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.

时间: 2024-11-08 21:01:05

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

Entity Framework学习(一) - Entity Framework Add and Attach and Entity States

Entity Framework Add and Attach and Entity States Entity Framewokr 新增.附加.实体状态 This topic will cover how to add and attach entities to a context and how Entity Framework processes these during SaveChanges. Entity Framework takes care of tracking the s

【译著】Code First :使用Entity. Framework编程(6)

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 Chapter6 Controlling Database Location,Creation Process, and Seed Data 第6章 控制数据库位置,创建过程和种子数据 In previous chapters you have

Entity Framework Code First 常用方法集成

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

Entity Framework中的连接管理

EF框架对数据库的连接提供了一系列的默认行为,通常情况下不需要我们太多的关注.但是,这种封装,降低了灵活性,有时我们需要对数据库连接加以控制. EF提供了两种方案控制数据库连接: 传递到Context的连接: Database.Connnection.Open(): 下面详解. 传递到Context的连接 EF6之前版本 有两个接受Connection的构造方法: public DbContext(DbConnection existingConnection, bool contextOwns

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

ADO.NET Entity Framework学习笔记(3)ObjectContext对象[转] 说明 ObjectContext提供了管理数据的功能 Context操作数据 AddObject 添加实体 将实体添加到集合中, 创建实体时,状态为EntityState.Detached 当调用AddObject将实体添加到Context时,状态为EntityState.Added myContext context = new myContext(); myTab r = new myTab(

了解Entity Framework中事务处理

Entity Framework 6以前,框架本身并没有提供显式的事务处理方案,在EF6中提供了事务处理的API. 所有版本的EF,只要你调用SaveChanges方法进行插入.修改或删除,EF框架会自动将该操作进行事务包装.这种方法无法对事务进行显式的控制,例如新建事务等,可能会造成事务的粒度非常大,降低效率.EF不会对查询进行事务包装. 从EF6开始,默认情况下,如果每次调用Database.ExecuteSqlCommand(),如果其不在存在于任何事务中,则会将该Command包装到一个

Entity Framework 6 Recipes 2nd Edition(9-3)译->找出Web API中发生了什么变化

9-3. 找出Web API中发生了什么变化 问题 想通过基于REST的Web API服务对数据库进行插入,删除和修改对象图,而不必为每个实体类编写单独的更新方法. 此外, 用EF6的Code Frist实现数据访问管理. 本例,我们模拟一个N层场景,用单独的客户端(控制台应用)来调用单独的基于REST服务的Web网站(WEB API应用) . 注意:每层使用单独的Visual Studio 解决方案, 这样更方便配置.调试和模拟一个N层应用. 假设有一个如Figure 9-3所示的旅行社和预订

Entity Framework 学习

Entity Framework 学习初级篇1--EF基本概况... 2 Entity Framework 学习初级篇2--ObjectContext.ObjectQuery.ObjectStateEntry.ObjectStateManager类的介绍... 7 Entity Framework 学习初级篇3-- LINQ TOEntities. 10 Entity Framework 学习初级篇4--EntitySQL. 17 Entity Framework 学习初级篇5--ObjectQ

让EF飞一会儿:如何用Entity Framework 6 连接Sqlite数据库

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 获取Sqlite 1.可以用NuGet程序包来获取,它也会自动下载EF6 2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.