本文转自:http://www.binaryintellect.net/articles/165bb877-27ee-4efa-9fa3-40cd0cf69e49.aspx

When you make any additions, modifications and deletions to an Entity  Framework DbSet and call SaveChanges(), EF starts a new transaction and executes  all the INSERT, UPDATE and DELETE operations inside that newly created  transaction. If the call to SaveChanges() succeeds the underlying transaction is  committed, otherwise the transaction is rolled back. In some cases you may want  that multiple calls to SaveChanges() be executed in the same transaction.  Luckily, Entity Framework 6 provides an easy way to accomplish the same.

Let's assume that you have an Entity Framework data model with Customer  entity as shown below:

Now suppose that you wrote the following code to add two Customer records to  the database.

NorthwindEntities db = new NorthwindEntities();

Customer obj1 = new Customer();
obj1.CustomerID = "ABCDE";
obj1.CompanyName = "Company 1";
obj1.ContactName = "Contact 1";
obj1.Country = "USA";
db.Customers.Add(obj1); db.SaveChanges();

Customer obj2 = new Customer();
obj2.CustomerID = "PQRST";
obj2.CompanyName = "Company 2";
obj2.ContactName = "Contact 2";
obj2.Country = "USA";
db.Customers.Add(obj2); db.SaveChanges();

In this case two calls to SaveChanges() are made. The first call adds the  first Customer to the database and the second call adds the other Customer to  the database. If the second call to SaveChanges() fails for some reason the  first Customer still gets added to the database because each call to SaveChanges()  runs in its own transaction.

Now let's modify this code as shown below:

using(NorthwindEntities db = new NorthwindEntities())
{
DbContextTransaction transaction = db.Database.BeginTransaction();

try
{
//insert a record
Customer obj1 = new Customer();
obj1.CustomerID = "ABCDE";
obj1.CompanyName = "Company 1";
obj1.ContactName = "Contact 1";
obj1.Country = "USA";
db.Customers.Add(obj1); //first call to SaveChanges() db.SaveChanges();

//insert another record
Customer obj2 = new Customer();
obj2.CustomerID = "PQRST";
obj2.CompanyName = "Company 2";
obj2.ContactName = "Contact 2";
obj2.Country = "USA";
db.Customers.Add(obj2); //second call to SaveChanges() db.SaveChanges();

transaction.Commit();
}
catch
{
transaction.Rollback();
}
}

Notice that the above code explicitly starts a transaction by calling  BeginTransaction() method on the Database property of the data context. The  BeginTransaction() returns a DbContextTransaction object which is stored in a  local variable. This object is used to either commit or rollback the transaction  later in the code.

The code then adds Customer entities as before and calls SaveChanges() after  each addition. This time since our code is explicitly creating a transaction,  both the calls to SaveChanges() are treated as the part of this transaction. If  both the calls to SaveChanges() are successful we call Commit() method of  DbContextTransaction object. If any of them fails we call the Rollback() method  of DbContextTransaction object.

You can test the above code by setting the CustomerID of the second Customer  to a string longer than five characters.

Note: There is also UseTransaction() method that can be used if you wish to couple EF  operations with plain ADO.NET transactions.

That's it for now! Keep coding !!

Bipin Joshi is a software consultant, trainer, author and a yogi having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, AngularJS, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Ajapa Meditation to interested individuals. To know more about him click here.

[转]Wrapping multiple calls to SaveChanges() in a single transaction的更多相关文章

  1. Enlisting multiple 1-phase aware participants in the same transaction

    In some cases it may be necessary to enlist participants that aren't two-phase commit aware into a t ...

  2. 从文件中读取字符-多次调用read characters from file multiple calls

    [抄题]: 接口:int read4(char * buf)一次从文件中读取 4 个字符.返回值是实际读取的字符数. 例如,如果文件中只剩下 3 个字符,则返回 3.通过使用read4 接口,实现从文 ...

  3. Oracle Applications Multiple Organizations Access Control for Custom Code

    档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...

  4. Entity Framework中DbContext结合TransactionScope提交事务的正确方式

    问: I would like know what is the best possible way to implement transactions with DBContext. In part ...

  5. postgresql大批量数据导入方法

    一直没有好好关注这个功能,昨天看了一下,数据库插入有瓶颈,今天研究了一下: 主要有以下方案: 1.使用copy从文件导入: copy table_001(a, b, "f", d, ...

  6. XA Transactions Restrictions on XA Transactions

    小结: 1.innodb支持XA事务: 2.XA协议作为资源管理器(数据库)与事务管理器的接口标准: 3.提交或者回滚的点:必须所有的组件被提交或者被回滚: 4.2阶段 PC-1,TM告知所有RM要准 ...

  7. Wrapping calls to the Rational Functional Tester API——调用Rational Functional Tester封装的API

    转自:http://www.ibm.com/developerworks/lotus/library/rft-api/index.html The Rational GUI automation to ...

  8. WCF Concurrency (Single, Multiple, and Reentrant) and Throttling

    http://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and Introduc ...

  9. Learning WCF Chapter1 Exposing Multiple Service Endpoints

    So far in this chapter,I have shown you different ways to create services,how to expose a service en ...

随机推荐

  1. Django的文件上传以及预览、存储

    思路: 文件上传通过前端的input标签,input设置display:none属性. 内容显示需要让前端通过<img>标签读取图片内容,可以通过<label>标签连接< ...

  2. Visual odometry and zed's IMU fusion on RTAB-Map

    "When using /camera/odom, you don't need to use visual_odometry node. rtabmap should be subscri ...

  3. jQuery定位导航滚动3

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. ASPxGridView后台实现隐藏新增按钮

    (ASPxGridView2.Columns[0] as GridViewCommandColumn).NewButton.Visible = false;//0应该代表的是NewButton所在的列 ...

  5. TFS解锁命令

    tf undo  /server:http://193.100.100.29:8080/tfs/EGSS  /workspace:HY-PC;hy   $/CTCS/TSAGS_TEAM/TestHY ...

  6. JavaScript学习笔记——4.数组

    数组(Array) 数组也是一个对象   数组中保存的内容我们称为元素 数组的操作 - 创建数组 - var arr = new Array();   - var arr = []; 例如:var m ...

  7. 品味Zookeeper之选举及数据一致性_3

    品味Zookeeper之选举及数据一致性 本文思维导图 前言 为了高可用和数据安全起见,zk集群一般都是由几个节点构成(由n/2+1,投票机制决定,肯定是奇数个节点).多节点证明它们之间肯定会有数据的 ...

  8. Luogu P1436 棋盘分割 暴力DP

    我的天,,,,,n=8,k<=15,,,这怕不是暴力DP+高维数组.... 开一个五维数组f[k][i][j][p][q]表示从(i,j)到(p,q)中分成k个矩形最小的平方和. 然后初始化时用 ...

  9. UESTC - 1724 GCD区间求和

    依然是神奇的欧拉函数 若GCD(n,i)=k 则GCD(n/k,i/k)=1, 令i/k=x,有GCD(n/k,x)=1, →k*GCD(n/k,x)=1中x的个数 = GCD(n,i)=k的和 范围 ...

  10. HDU 1232 (畅通工程) 并查集经典模板题

    Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...