In this article, we shall learn how to use transactions in ADO.NET Entity framework in simplified manner.
First thing first, What is transaction ?
As per MSDN, Transactions are groups of database commands that execute as a package. Using a transaction gives your application the ability to abort (roll back) all changes executed from within the transaction if any errors occur during any part of the transaction process. What this necessarily means that if we are executing three SQL commands in a group and if one of them fails, the database will not get affected (eg. if first two commands has executed successfully and the last one failed, the changes made by first two SQL commands will be rolled back and database will not be affected).
What is ADO.NET Entity Framework?
ADO.NET Entity Framework is an object-relational mapping (ORM) framework for the .NET application that can be used to perform Create, Read, Update and Delete operations in the database. It also helps us speedy development as we do not need to write ADO.NET code ourselves. We just need to add an ADO.NET Entity data model and write few lines of code to perform database operations.
The objective of this article is to demonstrate, how to use transactions in ADO.NET Entity framework. To achieve this, we are going to use
TransactionScope class that exists in System.Transactions namespace. To know more about TransactionScope and its use in normal coding, read this article.
Get 500+ ASP.NET web development Tips & Tricks and ASP.NET Online training here.
Using the code
To demonstrate using transactions in ADO.NET Entity Framework, I have created a simple .aspx page with two GridViews and a Button.
<asp:GridView ID="GridView1" runat="server" EnableViewState="false" />
<asp:GridView ID="GridView2" runat="server" EnableViewState="false" />
<asp:Button ID="fdas" runat="server" Text="Save with transaction" OnClick="SaveUsingLinqWithTransaction" />
Two GridViews list records from
tables (this is the simple table structure) separately. On click of the button, we are calling
method and below is the code of this method.
Namespace to use
protected void SaveUsingLinqWithTransaction(object sender, EventArgs e)
using (TransactionScope scope = new TransactionScope()) // exists in System.Transactions
using (DemoDatabaseDB db = new DemoDatabaseDB()) // Context object
PersonalDetail p = new PersonalDetail()
Active = true,
Age = int.MaxValue,
FirstName = "firstnamne",
LastName = "lastname"
DemoDatabaseModel.Menu m = new DemoDatabaseModel.Menu()
MenuDescription = "description1",
MenuName = "Menu1Menu1Menu1Menu1Menu1Menu1Menu21",
MenuUrl = "http://www.fundooVideo.com/",
ParentMenuId = 1,
MenuId = 1245
scope.Complete(); // transaction complete
catch (Exception ee)
In the above code snippet, we have instantiated
class with using block
. It ensures that when we are done with this object, it is disposed so we do not have memory leak problem.
DemoDatabaseDB is my context class of ADO.NET Entity Framework that has also been instantiated with using block. We are first trying to add a PersonalDetail data into the database so we have instantiated that class and set the value, to save this data to the database we have added this object into
AddObject method of the PersonalDetails object of the context object and called the SaveChanges() method.
Similarly, we have done the same for the Menu data as well. At last we have called the Complete() method.
Wrapping our ADO.NET Entity Framework commands inside the
TransactionScope ensures that all commands executed on the object actually temporarily affects the database and once the .Complete() method is called, these operations are marked as permanent.
Note that in case you have a AutoIncrement column in the database table and the transactions fails, the auto increment value will increase by 1 every time it fails.
As against the normal ADO.NET Transaction
, there is no Rollback()
method of the
here. Unless you call the Complete()
method, the transaction is not treated as complete and all the operations are rolled back automatically.
Is there any other way to achieve the transactions in ADO.NET Entity framework?
Yes, there is one more way to achieve the same thing explained here and Deeraj has written a nice article
on this long back.
Thanks for reading and hope this article was useful.
Keep learning and sharing knowledge on DotNetFunda.com website.
About the Author