使用Linq to SQL的NOLOCK [英] NOLOCK with Linq to SQL

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

问题描述

是否有可能使Linq2Sql在其SQL中发出NOLOCK?如果可以,怎么办?

解决方案

是的,所以这是条目

事实证明,您可以完成全部工作 读取未提交"的事务 使用旧的System.Transactions .NET 2.0中引入的名称空间. 这是一些示例代码:

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

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

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

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

马特·汉密尔顿-LINQ to SQL和NOLOCK提示:疯狂道具!

斯科特·汉塞尔曼(Scott Hanselman)的计算机Zen-将LINQ转换为SQL,将LINQ转换为... /p>

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:

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.

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
}

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.

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

InfoQ: Implementing NOLOCK with LINQ to SQL and LINQ to Entities

Matt Hamilton - LINQ to SQL and NOLOCK Hints : Mad Props!

Scott Hanselman's Computer Zen - Getting LINQ to SQL and LINQ to ...

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

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