使用 Linq to SQL 进行 NOLOCK [英] NOLOCK with Linq to SQL

查看:23
本文介绍了使用 Linq to SQL 进行 NOLOCK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以让 Linq2Sql 在其 SQL 中发出 NOLOCK?如果是这样,如何?

Is it possible to get Linq2Sql to emit a NOLOCK in its SQL? And if so, how?

推荐答案

是的,所以这里是条目 来自我的博客:

Yes it is, so here's the entry from my blog:

NOLOCK 提示本质上是与将查询包装在一个其隔离级别"为设置为读取未提交".它的意思是查询不关心是否有东西正在写入它正在读取的行 - 它会读取脏"数据并将其返回作为结果集的一部分.

The NOLOCK hint is essentially the same as wrapping a query in a transaction whose "isolation level" is set to "read uncommitted". It means that the query doesn't care if stuff is in the process of being written to the rows it's reading from - it'll read that "dirty" data and return it as part of the result set.

事实证明你可以做所有的事情读未提交"事务使用旧的 System.Transactions.NET 2.0 中引入的命名空间.下面是一些示例代码:

Turns out that you can do the whole "read uncommitted" transaction thing using the old System.Transactions namespace introduced in .NET 2.0. Here's some sample code:

using (var txn = new TransactionScope(
    TransactionScopeOption.Required, 
    new TransactionOptions
    {
        IsolationLevel = IsolationLevel.ReadUncommitted
    }
))
{
    // Your LINQ to SQL query goes here
}

所以我正在创建一个新的 TransactionScope对象并告诉它使用未提交读隔离级别.这现在在使用"语句中查询就像它的所有表都在阅读一样带有 NOLOCK 提示.

So I'm creating a new TransactionScope object and telling it to use a read-uncommitted isolation level. The query within the "using" statement now acts as if all its tables were reading with the NOLOCK hint.

以下是 Google 搜索linq sql nolock"的第一个结果:

Here are the first results from a Google search for "linq sql nolock":

InfoQ:使用 LINQ to SQL 和 LINQ to Entities 实现 NOLOCK

Matt Hamilton - LINQ to SQL 和 NOLOCK 提示:疯狂道具!

Scott Hanselman 的 Computer Zen - 使用 LINQ to SQL 和 LINQ to ...

这篇关于使用 Linq to SQL 进行 NOLOCK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆