I setup a DbContext where I have this code:
base.Database.ExecuteSqlCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
in the context constructor. I execute a stored procedure like this:
return base.Database.ExecuteSqlCommand("EXEC mystoredprocedure");
but I'm getting a deadlock because this executes in a read committed connection.
I'm asking is there any reason why the stored procedure would run in a read committed fashion when it is part of the same context that has the read uncommitted connection.
Try using TransactionScope instead as follows:
Or using Database.BeginTransaction:
EF wraps each
ExecuteSqlCommandin its own transaction by default. That's why the first call toSET ISOLATION LEVELdoes not extend the transaction to the 2nd call toEXEC proc. More about the working with transactions in EF in the link below: