如何在使用 LINQ 时使用 SQL Server 表提示?

发布于 2024-10-01 02:57:53 字数 133 浏览 0 评论 0原文

在使用LINQ时,如何使用Sql Server的表提示(例如“NOLOCK”)?

例如,我可以在 SQL 中编写“SELECT * from employee(NOLOCK)”。

我们如何使用 LINQ 编写相同的内容?

What is the way to use Sql Server's table hints like "NOLOCK" while using LINQ?

For example I can write "SELECT * from employee(NOLOCK)" in SQL.

How can we write the same using LINQ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攒一口袋星星 2024-10-08 02:57:53

以下是应用 NOLOCK 的方法: http://www.hanselman.com/blog/GettingLINQToSQLAndLINQToEntitiesToUseNOLOCK.aspx< /a>

(引用给后代,斯科特先生保留所有权利):

ProductsNewViewData viewData = new ProductsNewViewData();
using (var t = new TransactionScope(TransactionScopeOption.Required,
    new TransactionOptions { 
        IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted 
    }))
{
   viewData.Suppliers = northwind.Suppliers.ToList();
   viewData.Categories = northwind.Categories.ToList();
}

Here's how you can apply NOLOCK: http://www.hanselman.com/blog/GettingLINQToSQLAndLINQToEntitiesToUseNOLOCK.aspx

(Quote for posterity, all rights reserved by mr scott):

ProductsNewViewData viewData = new ProductsNewViewData();
using (var t = new TransactionScope(TransactionScopeOption.Required,
    new TransactionOptions { 
        IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted 
    }))
{
   viewData.Suppliers = northwind.Suppliers.ToList();
   viewData.Categories = northwind.Categories.ToList();
}
时光沙漏 2024-10-08 02:57:53

我强烈建议在使用 ReadUncommited 之前阅读有关 SQL Server 事务隔离模式的内容。
这是一篇非常好的读物

http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx

在许多情况下级别 ReadSnapshot 应该足够了。如果您确实需要它,您也可以使用

Set Transaction Isolation Level --levelHere

其他好主意为您的数据库设置默认事务隔离级别,包括将您的上下文包装在一个包装器中,该包装器使用所需的隔离级别封装每个调用。 (也许 95% 的时间你需要 nolock,5% 的时间你需要序列化)。它可以使用扩展方法或普通方法通过代码来完成,例如:

viewData.Categories = northwind.Categories.AsReadCommited().ToList();

它需要你的 IQueryable 并执行 Rob 提到的技巧。

希望有帮助
卢克

I STRONGLY recommend reading about SQL Server transaction isolation modes before using ReadUncommitted.
Here's a very good read

http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx

In many cases level ReadSnapshot should suffice. Also you if You really need it You can set default transaction isolation level for Your database by using

Set Transaction Isolation Level --levelHere

Other good ideas include packaging Your context in a wrapper that encapsulates each call using demanded isolation level. (maybe You need nolock 95% of the time and serializable 5% of the time). It can be done using using extension methods, or normal methods by code like:

viewData.Categories = northwind.Categories.AsReadCommited().ToList();

Which takes your IQueryable and does the trick mentioned by Rob.

Hope it helps
Luke

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文