Use Of Transaction In EntityFramework

The important use of transaction is that it can execute multiple operations in a single transaction. In other words, a transaction is used to perform operations in a single unit of work. Whenever we execute SaveChanges() extension method to insert, update or delete on the database this framework will wrap these operations in a transaction. Entity Framework transactions are a part of its internal architecture. Entity Framework starts a new transaction for each operation and completes the transaction when the operation finishes.

Introduction
 
The important use of transaction is that it can execute multiple operations in a single transaction. In other words, A transaction is used to perform operations in a single unit of work. Whenever we execute SaveChanges() extension method to insert, update or delete on the database this framework will wrap these operations in a transaction. Entity Framework transactions are a part of its internal architecture. Entity Framework starts a new transaction for each operation and completes the transaction when the operation finishes. We can achieve these all using  BeginTransaction() extension method of Entity framework and it has introduced in EF6.
BeginTransaction contains:
  • Commit
  • Rollback 
  • Underlying transaction
  • Dispose
Commit
 
COMMIT Extension method is used to end our current transaction and make permanent all changes performed in the transaction
 
Rollback
 
Rolls back an extension method for an explicit or implicit transaction to the beginning of the transaction. Main use is to erase all data modifications made from the start of the transaction 
 
Dispose
 
Dispose extension method is used to Releases the resources that are held by the respective object.
 
Underlying transaction
 
It's used to provide the information about sqlTransaction and sqlConnection. 
 
Okay! Let us use these all following examples
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace Transaction {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             using(CSharpCornerEntities databaseEntities = new CSharpCornerEntities()) {  
  10.                 using(var transaction = databaseEntities.Database.BeginTransaction()) {  
  11.                     try {  
  12.                         var TransactionInfo = transaction.UnderlyingTransaction;  
  13.                         if (TransactionInfo.Connection != null) {  
  14.                             Console.WriteLine("UnderlyingTransaction Has Connection info.,");  
  15.                         }  
  16.                         Employee employee = new Employee();  
  17.                         employee.Designation = "Software Engineer";  
  18.                         employee.Location = "Noida";  
  19.                         employee.Name = "Record 1";  
  20.                         databaseEntities.Employees.Add(employee);  
  21.                         Employee employee1 = new Employee();  
  22.                         employee1.Designation = "Software Engineer";  
  23.                         employee1.Location = "Mumbai";  
  24.                         employee1.Name = "Record 2";  
  25.                         databaseEntities.Employees.Add(employee1);  
  26.                         databaseEntities.SaveChanges();  
  27.                         transaction.Commit();  
  28.                         var TransactionInfo1 = transaction.UnderlyingTransaction;  
  29.                         if (TransactionInfo1.Connection == null) {  
  30.                             Console.WriteLine("UnderlyingTransaction is not have connection information");  
  31.                         }  
  32.                         foreach(var item in databaseEntities.Employees) {  
  33.                             Console.WriteLine("Name : " + item.Name);  
  34.                         }  
  35.                     } catch (Exception ex) {  
  36.                         transaction.Rollback();  
  37.                     }  
  38.                 }  
  39.             }  
  40.         }  
  41.     }  
  42. }  
Now run your application
 
 
 
If any error occurs during this transaction it will rollback all operations using Rollback() extension method in a catch block.
 
I hope it's helpful