Does changing isolation level using TransactionScope in LINQ to SQL for NOLOCK?

I am testing TransactionScope with parameters to set the isolation level for ReadUncommitted to fulfill certain requests as such. However, I see that because the isolation level is set to the connection when the connection is reused for other requests, the isolation level is still ReadUncommitted instead of resetting to ReadCommitted by default.

For many suggestions, I looked at the new NoLock methods as extensions as follows:

public static class QueryableExtensions
{
    static TransactionScope CreateNoLockTransaction()
    {
        return new TransactionScope(TransactionScopeOption.Required, new TransactionOptions
        {
            IsolationLevel = IsolationLevel.ReadUncommitted
        });
    }

    public static T[] ToNoLockArray<T>(this IEnumerable<T> query)
    {
        using (var ts = CreateNoLockTransaction())
        {
            return query.ToArray();
        }
    }

    public static List<T> ToNoLockList<T>(this IEnumerable<T> query)
    {
        using (var ts = CreateNoLockTransaction())
        {
            return query.ToList();
        }
    }

    public static int NoLockCount<T>(this IEnumerable<T> query)
    {
        using (var ts = CreateNoLockTransaction())
        {
            return query.Count();
        }
    }
}

Then I needed to check the isolation level for various requests that I run both inside the transaction scope and without it. To do this, I started to execute the following query using the query context:

db.ExecuteQuery<int>("select cast(transaction_isolation_level as int) from sys.dm_exec_sessions where session_id = @@SPID").FirstOrDefault();

NoLock 2. NoLock , 1.

, TransactionScope , , ReadUncommitted? , , ?

+4
2

ts.Complete()

0

, . LINQ to SQL, TransactionScope , sql-spid .

, :

dbContext.ExecuteCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");

var Data = query.ToList();

dbContext.ExecuteCommand("SET TRANSACTION ISOLATION LEVEL READ COMMITTED");

, , , , sql, , , .

LINQ select, .

0

All Articles