0

We have some instances where multiple applications depend on the completed work of the others. We start and commit our transactions via the Database property of our Entity Framework DatabaseContext.

Like this:

public void StartTransaction(IsolationLevel isolationLevel = IsolationLevel.Serializable)
{           
    CheckTransactionNotActive();

    if (!Database.Connection.State.Equals(ConnectionState.Open))
    {
        Database.Connection.Open();
    }

    _transaction = Database.BeginTransaction(isolationLevel);
}

This results in the following when watching the created transaction in the debugger:

Code transaction

However SQL Server Profiler shows another picture:

Db transaction

How is this possible, and how do we prevent it!?

Update

rewrote my test to use TransactionScope: The following code is executed from multiple tasks

        var context = new MyDbContext();
        var myRepository = new MyRepository(context);

        using (
            TransactionScope scope =
                new TransactionScope(
                    TransactionScopeOption.RequiresNew,
                    new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.Serializable })
             )
        {
            var x = my.GetAll().First();
            Console.WriteLine("before " + x.Count);
            x.count = x.count + 1;
            Console.WriteLine("after " + x.Count);
            context.SaveChanges(false);
            scope.Complete();
        }

The output shows that my requested transaction isolation level is not honored

enter image description here

k.c.
  • 1,755
  • 1
  • 29
  • 53
  • This might give you some insight into why this is happening: http://stackoverflow.com/questions/31273933/setting-transaction-isolation-level-in-net-entity-framework-for-sql-server – TTeeple Aug 04 '16 at 15:27
  • @TTeeple It is somewhat helpful, but I'm not using transaction scope, I'm starting an explicit database transaction. – k.c. Aug 05 '16 at 06:24

0 Answers0