59

I am using Entity Framework 6 in an MVC 5 project. As you're aware of, SELECT queries in SQL Server perform faster and more efficient if we use WITH (NOLOCK) in them. I checked out a few SQL SELECT statements generated by Entity Framework 6 and realized that none of them contain NOLOCK.

I do not want to use transactions in my fetch operations to read from uncommitted transactions.

How can I enforce EF 6 to use NOLOCK in the underneath generated SELECT statements?

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
Arash
  • 3,628
  • 5
  • 46
  • 70
  • 12
    NOLOCK is NOT a magic go fast button. It brings some particularly nasty things along with the slight performance boost. Are you ok with duplicate and/or missing rows from your queries? Are you cool with incredibly difficult to reproduce and fix bugs? Read this article for more details. http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx – Sean Lange Jul 10 '14 at 19:42
  • 1
    Scott Hanselman addressed NOLOCK with regards to LINQ to SQL way back in '08. It might be worth a read. http://www.hanselman.com/blog/GettingLINQToSQLAndLINQToEntitiesToUseNOLOCK.aspx – Rick Liddle Jul 10 '14 at 19:43
  • 3
    Are you my manager from 6 years ago who thought (nolock) was the answer to everything...and not proper db index turning ? – granadaCoder Jul 10 '14 at 20:54
  • 6
    As a matter of fact, I am fine with reading uncommitted data. – Arash Jul 10 '14 at 21:41
  • 6
    I've worked at companies where with(nolock) is required on all but the most critical queries, and you will get fired if you don't use it. You can argue till you're blue in the face. Same people argue that you can't use SELECT * in an exists clause, despite the fact that select doesn't return any rows... blue in the face... However, I do understand the point. It's not a magic gofast button for MY queries, they don't want my queries locking tables for everyone else. – Erik Funkenbusch Jul 10 '14 at 23:16
  • possible duplicate of [Entity Framework with NOLOCK](http://stackoverflow.com/questions/926656/entity-framework-with-nolock) – Matt Sep 02 '15 at 11:34
  • why don't you want to use transactions ? – eran otzap Jul 11 '16 at 21:17
  • 1
    fixed link from @SeanLange 's comment https://learn.microsoft.com/en-US/archive/blogs/davidlean/sql-server-nolock-hint-other-poor-ideas – Amit G Feb 14 '20 at 16:49
  • 1
    @AmitG thanks. MS archived a lot of their blogs several months ago. Not really sure why. Here is another article on the topic that is not as likely to change. https://blogs.sentryone.com/aaronbertrand/bad-habits-nolock-everywhere/ – Sean Lange Feb 14 '20 at 16:58

5 Answers5

79

First of all... You should NEVER EVER use NOLOCK for each and every SQL Statement. It could compromise the integrity of your data.

It’s like any other query hint a mechanism you should only use when you do something out of the ordinary.

There is no way to tell the EF Provider to render the NoLock hint. If you really need to read uncommitted data you have the following option.

  1. Write your own EntityFramework Provider.

  2. Use a Command Interceptor to modify the statement before it is executed. http://msdn.microsoft.com/en-us/data/dn469464.aspx

  3. Use a TransactionScope with IsolationLevel.ReadUncommited.

I know you said you do not want to use Transactions but it's the only out-of-the box way to read uncommitted data. Also it does not produce much overhead as each statement in SQL Server “implicitly” runs in a transaction.

using (new TransactionScope(
                    TransactionScopeOption.Required, 
                    new TransactionOptions 
                    { 
                         IsolationLevel = IsolationLevel.ReadUncommitted 
                    })) 
{
        using (var db = new MyDbContext()) { 
            // query
        }
}

EDIT: It's important to note also that NOLOCK for Updates and Deletes (selects remain intact) has been Deprecated by Microsoft as of SQL Server 2016 and and will be removed in 'a' future release.

https://learn.microsoft.com/en-us/sql/database-engine/deprecated-database-engine-features-in-sql-server-2016?view=sql-server-2017

Spevy
  • 1,325
  • 9
  • 22
codeworx
  • 2,715
  • 13
  • 12
  • 6
    Yes, I totally understand the possible consequences of using NOLOCK in SELECT statements. We are totally fine with using NOLOCK against a table retaining a huge chunk of data because the WHERE clause knows where to pick up data from the "already-committed" portion. That particular table is used to INSERT/UPDATE a very huge chunk of data in an ongoing transaction that might take several minutes to finish. However we'd like to use another area of the data table while the transaction is in progress. Our bottom line is that we do not want to lose speed of our application. – Arash Jul 11 '14 at 15:11
  • @ErikFunkenbusch that's not an option Erik. Thanks though. – Arash Jul 11 '14 at 17:17
  • 2
    @Arash If that is your problem, why don't you disable table-level (and page-level) locking instead of using NOLOCK? The effect should be the same if the read and the write don't touch the same data. – erikkallen Feb 06 '15 at 12:00
  • 1
    For "Option 3" mentioned above as solutions which is probably easier than other two has example in other quesiton: http://stackoverflow.com/questions/926656/entity-framework-with-nolock – Raghu A Mar 04 '15 at 17:19
  • Do note though that "read uncommitted" and "nolock" are more or less totally different things - in particular, the "read uncommitted" example in this answer can still take locks. – Roman Starkov May 22 '17 at 14:52
  • 8
    "...NEVER EVER..." and "any other query hint a mechanism you should only use when you do something out of the ordinary" - who says that he is not doing something out of the ordinary? Hints have to be used carefully and with understanding, but they are not evil. – i486 Mar 06 '18 at 14:58
  • about "NEVER EVER". You should use it in SELECT statements when you read data which might be temporary locked by heavy operations like nightly updates etc. If you are not in the situation when exact data is very important (stock market trading currency value), and you just need data without waiting locks -- use it. It is normal practice. – Dmitry Jul 03 '20 at 13:01
  • 3
    Since some people seem to have a problem with my usage of NEVER EVER in the answer, I would like to clarify what I exactly meant. I used this phrase in the same way as It is used in the following Sentence: You should NEVER EVER attempt to land a plane in the Hudson river, UNLESS both engines are down and the alternative is crashing into midtown Manhattan. – codeworx Oct 01 '20 at 13:26
  • A bit overly dramatic. I'd say don't attempt to create something that causes every read to be a dirty read. Sometimes you don't care if data is dirty or not. E.g when populating a drop down, it doesn't matter if every entry in the drop is valid, as long as you have validation on what's selected from the drop down. Even if you don't use NOLOCK to read such data, you'll still have concurrency issues with the data loaded becoming out dated in-between the load and use of the data. – Mick Jul 07 '22 at 02:11
21

You can use a workaround that don't use transaction scopes for each query. If you run the code below, ef will use the same transaction isolation level for the same Server Process ID. Since Server Process ID does not change in same request, only one call for each request is sufficient. This also works in EF Core.

this.Database.ExecuteSqlCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
Cem Mutlu
  • 1,969
  • 1
  • 24
  • 24
  • 14
    There is a big risk involved with this solution if your version of SQL Server is older than Sql Server 2014 because this set the Isolation Level for the whole connection. Once done with the connection, it will return to the pool of connections and the next call that will grab this specific connection will execute with that isolation level as well, which can be bad. Sql Server 2014 and above reset the isolation level when returning the connection to the pool. – AXMIM Oct 26 '18 at 18:14
  • axmim. thank you for this hint/reminder. i think my answer and "SetEndTransaction" addresses this. at the same time, I would have found this later-than-sooner without your comment. – granadaCoder Mar 24 '21 at 19:09
17

I agree with what codeworx says in a way that Read Uncommitted can be dangerous. If you can live with dirty reads, go for it.

I found a way to make this work without changing anything in the current queries.

You need to create a DbCommandInterceptor like this:

public class IsolationLevelInterceptor : DbCommandInterceptor
{
    private IsolationLevel _isolationLevel;

    public IsolationLevelInterceptor(IsolationLevel level)
    {
        _isolationLevel = level;
    }



    //[ThreadStatic]
    //private DbCommand _command;


    public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {
        SetTransaction(command);

    }

    public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        SetTransaction(command);
    }

    public override void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        SetTransaction(command);
    }




    private void SetTransaction(DbCommand command)
    {
        if (command != null)
        {
            if (command.Transaction == null)
            {
                var t = command.Connection.BeginTransaction(_isolationLevel);
                command.Transaction = t;
                //_command = command;
            }
        }
    }

}

then at the cctor (static contructor of your dbcontext) just add the interceptor to the DbInfrastructure of entity framework collections.

DbInterception.Add(new IsolationLevelInterceptor(System.Data.IsolationLevel.ReadUncommitted));

this will for each command that EF sends to the store, wraps over a transaction with that Isolation Level.

In my case worked well, since we write data through an API where that data is not based on the readings from the database. (data can be corrupted because of the dirty reads) and so worked fine.

granadaCoder
  • 26,328
  • 10
  • 113
  • 146
gds03
  • 1,349
  • 3
  • 18
  • 39
  • I had to change the SetTransaction to also check the connection for an existing transaction because EF can use the same connection for multiple commands. Used this example to get the transaction from the connection: https://stackoverflow.com/a/12284267/169060 – Justin Fisher Sep 11 '17 at 15:06
  • wouldn't this make it so _every_ query is reading uncommitted rows? how can you make this work for only specific queries? – jtate Sep 23 '19 at 15:23
6

In our project we use combination of the second and third solutions, suggested by @Cem Mutlu and @anotherNeo.

Experiment with Sql Profiler showed that we have to use pair of commands:

  • READ UNCOMMITTED
  • READ COMMITTED

because NET reuse connections via SqlConnectionPool

internal class NoLockInterceptor : DbCommandInterceptor
{
    public static readonly string SET_READ_UNCOMMITED = "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED";
    public static readonly string SET_READ_COMMITED = "SET TRANSACTION ISOLATION LEVEL READ COMMITTED";

    public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        if (!interceptionContext.DbContexts.OfType<IFortisDataStoreNoLockContext>().Any())
        {
            return;
        }

        ExecutingBase(command);
    }

    public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {
        if (!interceptionContext.DbContexts.OfType<IFortisDataStoreNoLockContext>().Any())
        {
            return;
        }

        ExecutingBase(command);
    }

    private static void ExecutingBase(DbCommand command)
    {
        var text = command.CommandText;
        command.CommandText = $"{SET_READ_UNCOMMITED} {Environment.NewLine} {text} {Environment.NewLine} {SET_READ_COMMITED}";
    }
}
StuS
  • 817
  • 9
  • 14
2

First off, please upvote gds03's answer. Because I would not have gotten this far without it.

I have contributed to "closing out the transaction" and getting the timing right for IDataReader/DbDataReader situations too. Basically, with IDataReader/DbDataReader situations, you do NOT close the transaction on the "ReaderExecuted"(Async) methods (emphasis on the "ed" of Executed), but let it "fall through" to (override of) DataReaderDisposing.

But if you read some of the other answers (here) (and comments), I think SetEndTransaction is an important part of .. not getting voodoo from the connection-pool (if you don't probably close out the transaction with the (for me) read-uncommitted).

using System.Data;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace My.Interceptors
{
    public class IsolationLevelInterceptor : DbCommandInterceptor
    {
        private IsolationLevel _isolationLevel;

        public IsolationLevelInterceptor(IsolationLevel level)
        {
            _isolationLevel = level;
        }

        //[ThreadStatic]
        //private DbCommand _command;


        public override InterceptionResult DataReaderDisposing(DbCommand command, DataReaderDisposingEventData eventData, InterceptionResult result)
        {
            InterceptionResult returnItem = base.DataReaderDisposing(command, eventData, result);
            SetEndTransaction(command);
            return returnItem;
        }




        public override InterceptionResult<DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result)
        {
            SetStartTransaction(command);
            InterceptionResult<DbDataReader> returnItem = base.ReaderExecuting(command, eventData, result);
            return returnItem;
        }


        public override DbDataReader ReaderExecuted(DbCommand command, CommandExecutedEventData eventData, DbDataReader result)
        {
            DbDataReader returnItem = base.ReaderExecuted(command, eventData, result);
            //SetEndTransaction(command); // DO NOT DO THIS HERE .. datareader still open and working .. fall back on DataReaderDisposing... you don't really need this override, but left in to show the possible issue.
            return returnItem;
        }


        public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default)
        {
            SetStartTransaction(command);
            ValueTask<InterceptionResult<DbDataReader>> returnItem = base.ReaderExecutingAsync(command, eventData, result, cancellationToken);
            return returnItem;
        }


        public override ValueTask<DbDataReader> ReaderExecutedAsync(DbCommand command, CommandExecutedEventData eventData, DbDataReader result, CancellationToken cancellationToken = default)
        {
            ValueTask<DbDataReader> returnItem = base.ReaderExecutedAsync(command, eventData, result, cancellationToken);
            //SetEndTransaction(command); // DO NOT DO THIS HERE .. datareader still open and working .. fall back on DataReaderDisposing... you don't really need this override, but left in to show the possible issue.
            return returnItem;
        }


        public override InterceptionResult<object> ScalarExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<object> interceptionContext)
        {
            SetStartTransaction(command);
            InterceptionResult<object> returnItem = base.ScalarExecuting(command, eventData, interceptionContext);
            return returnItem;
        }

        public override object ScalarExecuted(DbCommand command, CommandExecutedEventData eventData, object result)
        {
            SetEndTransaction(command);
            object returnItem = base.ScalarExecuted(command, eventData, result);
            return returnItem;
        }


        public override ValueTask<InterceptionResult<object>> ScalarExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<object> result, CancellationToken cancellationToken = default)
        {
            SetStartTransaction(command);
            ValueTask<InterceptionResult<object>> returnItem = base.ScalarExecutingAsync(command, eventData, result, cancellationToken);
            return returnItem;
        }


        public override ValueTask<object> ScalarExecutedAsync(DbCommand command, CommandExecutedEventData eventData, object result, CancellationToken cancellationToken = default)
        {
            SetEndTransaction(command);
            ValueTask<object> returnItem = base.ScalarExecutedAsync(command, eventData, result, cancellationToken);
            return returnItem;
        }


        /* start maybe not needed on queries that only do "reading", but listed here anyways */

        public override InterceptionResult<int> NonQueryExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<int> result)
        {
            SetStartTransaction(command);
            InterceptionResult<int> returnItem = base.NonQueryExecuting(command, eventData, result);
            return returnItem;
        }

        public override int NonQueryExecuted(DbCommand command, CommandExecutedEventData eventData, int result)
        {
            int returnValue = base.NonQueryExecuted(command, eventData, result);
            SetEndTransaction(command);
            return returnValue;
        }

        public override ValueTask<InterceptionResult<int>> NonQueryExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)
        {
            SetStartTransaction(command);
            ValueTask<InterceptionResult<int>> returnItem = base.NonQueryExecutingAsync(command, eventData, result, cancellationToken);
            return returnItem;
        }

        public override ValueTask<int> NonQueryExecutedAsync(DbCommand command, CommandExecutedEventData eventData, int result, CancellationToken cancellationToken = default)
        {
            ValueTask<int> returnValue = base.NonQueryExecutedAsync(command, eventData, result, cancellationToken);
            SetEndTransaction(command);
            return returnValue;
        }

        /* end maybe not needed on queries that only do "reading", but listed here anyways */

        private void SetStartTransaction(DbCommand command)
        {
            if (command != null)
            {
                if (command.Transaction == null)
                {
                    DbTransaction t = command.Connection.BeginTransaction(_isolationLevel);
                    command.Transaction = t;
                    //_command = command;
                }
            }
        }

        private void SetEndTransaction(DbCommand command)
        {
            if (command != null)
            {
                if (command.Transaction != null)
                {
                    command.Transaction.Commit();
                    //_command = command;
                }

                command.Dispose();
            }
        }

    }
}

This article was helpful in "seeing" all the "ing" and "ed" methods.

https://lizzy-gallagher.github.io/query-interception-entity-framework/

Please note my answer is EntityFrameworkCore (3.1.+), but I think it will "back port" to EF-for-DN-Framework.

The more important parts of my answer is the "timing" of certain methods...especially the IDataReader/DbDataReader methods.

granadaCoder
  • 26,328
  • 10
  • 113
  • 146