简体   繁体   中英

C# Adding a NOLOCK to a user-defined EF query

I have the following user-defined query:

        var outerRingEntities = await Databases.OuterRing.Set<TOuterEntity>()
                                .AsNoTracking()
                                .Where(transformer.ShouldQueryItem)
                                .Where(x => LastSyncTime == null || x.ChangedDate > LastSyncTime)
                                .OrderBy(p => p.ChangedDate)
                                .Skip(index)
                                .Take(_pageSize)
                                .ToListAsync(cancellationToken);

The objective would be to add a "NOLOCK" to this query. For that I have researched and tried so many different things and I still don't have a solution:

Attempt 1: I looked at the following link: How to use SqlAzureExecutionStrategy and "Nolock"

I was able to add SuspendableSqlAzureExecutionStrategy but the only problem is with the System.Runtime.Remoting.Messaging . I was able to locate this dll in .NET framework 4.8 but still have issues with the messaging. After modifying the classes, I am still getting the following error:

"The configured execution strategy 'SqlServerRetryingExecutionStrategy' does not support user initiated transactions. Use the execution strategy returned by 'DbContext.Database.CreateExecutionStrategy()' to execute all the operations in the transaction as a retriable unit."

Attempt 2: I followed the instruction written in this blog: https://dotnetdocs.ir/Post/38/implementing-nolock-in-entityframework-

Following the instructions on the blog, I modified the code like this:

    var outerRingEntities = await Databases.OuterRing.Set<TOuterEntity>()
                            .AsNoTracking()
                            .Where(transformer.ShouldQueryItem)
                            .Where(x => LastSyncTime == null || x.ChangedDate > LastSyncTime)
                            .OrderBy(p => p.ChangedDate)
                            .Skip(index)
                            .Take(_pageSize)
                            .ToListWithNoLockAsync(cancellationToken);

From this query, we can see that I am using an extension method and this method is defined in the link provided.

But I am still getting the same error that I got in the first attempt.

Is there a way for me to achieve my objective such that I execute the query as well as add a nolock to the EF query. Please advise.

Thanks in advance.

  1. NOLOCK is evil. Don't do this. Use READ COMMITTED SNAPSHOT database option or SNAPSHOT isolation level insted.

  2. If you open the DbContext's database connection it will remain open for the duration of the DbContext lifetime. So you can start a ReadUncommited transaction , or change the transaction isolation level in TSQL and subsequent queries will run with dirty reads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM