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:
However SQL Server Profiler shows another picture:
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