Transactions using Entity Framework

Introduction

In this blog we will see how to use transactions in entity framework.

Step 1: Create asp.net web application

Webform1.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="EF_Transcation_Support.WebForm1" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.     <table>  
  13.                 <tr>  
  14.                     <td>  
  15.                         <asp:Label ID="Label1" runat="server" Text="EF Transaction" Font-Bold="true"></asp:Label>  
  16.                     </td>  
  17.                 </tr>  
  18.             </table>  
  19.             <br />  
  20.             <br />  
  21.             <table>  
  22.                 <tr>  
  23.                     <td colspan="2">  
  24.                         <asp:Button ID="Button1" runat="server" Text="Insert Data"  
  25.                             BackColor="Orange" Font-Bold="true" OnClick="Button1_Click" />  
  26.                         <br />  
  27.                         <br />  
  28.                     </td>  
  29.                 </tr>  
  30.             </table>  
  31.     </div>  
  32.     </form>  
  33. </body>  
  34. </html>

Webform1.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace EF_Transcation_Support  
  9. {  
  10.     public partial class WebForm1 : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.   
  15.         }  
  16.   
  17.         protected void Button1_Click(object sender, EventArgs e)  
  18.         {  
  19.             using (EmployeeDBEntities empContext = new EmployeeDBEntities())  
  20.             {  
  21.                 using (var transaction = empContext.Database.BeginTransaction())  
  22.                 {  
  23.                     try  
  24.                     {  
  25.   
  26.                         Employee employee = new Employee();  
  27.                         employee.Id = 1;  
  28.                         employee.FirstName = "James";  
  29.                         employee.LastName = "Robert";  
  30.                         employee.DeptId = 1;  
  31.                         empContext.Employees.Add(employee);  
  32.                         empContext.SaveChanges();  
  33.   
  34.                         Department dept = new Department();  
  35.                         dept.DeptId = 1;  
  36.                         dept.Name = "IT";  
  37.                         empContext.Departments.Add(dept);  
  38.                         empContext.SaveChanges();  
  39.   
  40.                         transaction.Commit();  
  41.                     }  
  42.                     catch (Exception ex)  
  43.                     {  
  44.                         transaction.Rollback();  
  45.                     }  
  46.                 }  
  47.            }  
  48.         }  
  49.     }  
  50. }

Output of the application looks like this

Summary

In this blog we have seen how we can use transactions in entity framework. Happy coding.

Next Recommended Reading Entity Framework vs LINQ to SQL

MVC Corporation
MVC Corporation is consulting and IT services based company.