Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » ADO.NET » Downloads
 
Add your own
alternative version

Transaction Isolation in ADO.NET Entity Framework

, 15 May 2011 CPOL
This article presents an example on how to control the transaction isolation level in ADO.NET Entity Framework.
EFTransactionExample.zip
EFTransactionExample
EFTransactionExample
EFTransactionExample.suo
bin
BindingUtilities
EmployeeModel.edmx
Models
Properties
Settings.settings
ViewModels
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Transactions;

namespace EFTransactionExample.Models
{
    class ApplicationModel
    {
        // Get all the information for the employees
        public IEnumerable<Employee> GetEmployeeInformation()
        {
            EmployeeSalaryEntities context = new EmployeeSalaryEntities();
            return context.Employees.AsEnumerable<Employee>();
        }

        // Set an employee's salary to a value
        public void SetEmployeeSalary(int employeeID,
            string employeeName, int salary)
        {
            EmployeeSalaryEntities context = new EmployeeSalaryEntities();
            var employee = context.Employees.Where(e => e.ID == employeeID)
                .SingleOrDefault<Employee>();

            if (employee == null)
            {
                employee = new Employee()
                {
                    ID = employeeID,
                    Name = employeeName,
                    Salary = salary
                };

                context.Employees.AddObject(employee);
            }
            employee.Salary = salary;
            context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
        }

        public void RaiseEmployeeSalaryNonSerialized(int employeeID, int amount)
        {
            // Read employee information
            EmployeeSalaryEntities context = new EmployeeSalaryEntities();
            var employee = context.Employees.Where(e => e.ID == employeeID)
                .Single<Employee>();

            // sleep a while
            System.Threading.Thread.Sleep(5000);

            // Update the salary
            employee.Salary = employee.Salary + amount;
            context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
        }

        public void RaiseEmployeeSalarySerialized(int employeeID, int amount)
        {
            TransactionOptions options = new TransactionOptions();
            options.IsolationLevel = IsolationLevel.Serializable;
            using (TransactionScope scope
                = new TransactionScope(TransactionScopeOption.Required, options))
            using (EmployeeSalaryEntities context = new EmployeeSalaryEntities())
            {
                // Open the database connection explicitly
                context.Connection.Open();

                // Read employee information
                var employee = context.Employees.Where(e => e.ID == employeeID)
                    .Single<Employee>();

                // sleep a while
                System.Threading.Thread.Sleep(5000);

                // Update the salary
                employee.Salary = employee.Salary + amount;
                context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

                scope.Complete();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dr. Song Li

United States United States
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150301.1 | Last Updated 15 May 2011
Article Copyright 2011 by Dr. Song Li
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid