简体   繁体   English

C# 向用户定义的 EF 查询添加 NOLOCK

[英]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.目标是向该查询添加“NOLOCK”。 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"尝试1:我查看了以下链接: How to use SqlAzureExecutionStrategy and "Nolock"

I was able to add SuspendableSqlAzureExecutionStrategy but the only problem is with the System.Runtime.Remoting.Messaging .我能够添加SuspendableSqlAzureExecutionStrategy但唯一的问题是System.Runtime.Remoting.Messaging I was able to locate this dll in .NET framework 4.8 but still have issues with the messaging.我能够在 .NET Framework 4.8 中找到此 dll,但消息传递仍然存在问题。 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-尝试 2:我按照此博客中的说明进行操作: 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.有没有办法让我实现我的目标,以便我执行查询以及向 EF 查询添加一个 nolock。 Please advise.请指教。

Thanks in advance.提前致谢。

  1. NOLOCK is evil. NOLOCK是邪恶的。 Don't do this.不要这样做。 Use READ COMMITTED SNAPSHOT database option or SNAPSHOT isolation level insted.使用 READ COMMITTED SNAPSHOT 数据库选项或设置的 SNAPSHOT 隔离级别。

  2. If you open the DbContext's database connection it will remain open for the duration of the DbContext lifetime.如果您打开 DbContext 的数据库连接,它将在 DbContext 生命周期内保持打开状态。 So you can start a ReadUncommited transaction , or change the transaction isolation level in TSQL and subsequent queries will run with dirty reads.因此,您可以启动 ReadUncommited事务,或更改 TSQL 中的事务隔离级别,后续查询将运行脏读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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