Go Wrapping multiple calls to SaveChanges () inch a single transaction

Source: Internet
Author: User

This article transferred from: http://www.binaryintellect.net/articles/165bb877-27ee-4efa-9fa3-40cd0cf69e49.aspx

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

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

Now suppose this wrote the following code to add a 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";d B. Customers.add (OBJ1); db. SaveChanges (); Customer OBJ2 = new Customer (); Obj2. CustomerID = "Pqrst"; obj2.companyname = "Company 2"; Obj2. ContactName = "Contact 2"; obj2. Country = "USA";d B. Customers.add (OBJ2); db. SaveChanges ();

In this case, calls to SaveChanges () is made. The first call is 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 EAC H 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 The above code explicitly starts a transaction by calling BeginTransaction () method on the Database of the data context. The BeginTransaction () returns a Dbcontexttransaction object which is stored in a local variable. This object was used to either commits or rollback the transaction later in the code.

The code then adds Customer entities as before and calls SaveChanges () through each addition. This time since we code is explicitly creating a transaction, both the calls to SaveChanges () was treated as the part of This transaction. If both the calls to SaveChanges () is successful we call Commit () method of Dbcontexttransaction object. If any of them fails we call the Rollback () method of the 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 so can be used if you wish to couple EF operations with plain ADO Actions.

That ' s it for now! Keep Coding!!

Bipin Joshiis a software consultant, trainer, author and a Yogi have 21+ yearsof experience in software development. HE conducts online courses in ASP. Mvc/core, JQuery, AngularJS, and Design Patterns. He is a published author and have authored or co-authored books for Apress and Wrox Press. Having embraced Yoga A-from life he also teaches Ajapa meditation to interested individuals. To know more on him click here.

Go Wrapping multiple calls to SaveChanges () inch a single transaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.