博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LINQ to SQL with NOLOCK
阅读量:7140 次
发布时间:2019-06-28

本文共 1337 字,大约阅读时间需要 4 分钟。

If your project uses LINQ to SQL for your database access, you may want to use NOLOCK to set the isolation level to ReadUncommitted on your Select statements. The problem is that LINQ does not understand query hints. The way this works with LINQ is by running your Select statements under the context of a transaction scope, as follows:

string type = string.Empty;using (DataContext ctx = new DataContext(ConnectionString)){  using (var tx = new System.Transactions.TransactionScope(TransactionScopeOption.Required,         new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted }))  {     var row = (from record in ctx.Log                where record.Id == 99999                select record).FirstOrDefault();     type = row.Type;  }}

The code here creates a transaction scope and sets the isolation level to ReadUncommitted (NOLOCK). The Select statement runs under the scope of the transaction, and it returns the value from a query. The using clause ensures that both the transactions as well as the data context are disposed once the code goes out of scope.

There is also the preferable option to use Stored Procedures to eliminate the need to use transaction scope. In the Stored Procedure, you have the flexibility to use query hints.

原文链接:

转载于:https://www.cnblogs.com/EddyPeng/archive/2013/04/07/3003601.html

你可能感兴趣的文章
如何自动搞定全站图片的alt属性?
查看>>
畅谈云原生(下):云原生的飞轮理论
查看>>
如何进行5万并发用户负载测试?
查看>>
中台之上(十一):企业级业务架构设计的“五难”
查看>>
支撑AIOps的运维角色和技能有哪些?
查看>>
座谈会:Apache基金会那些事儿
查看>>
科普 | 你必须了解的漏洞利用缓解及对抗技术
查看>>
AWS Amplify Console:赋予应用程序快速部署的能力
查看>>
JQuery基础——选择器
查看>>
架构周报| 浅析MySQL JDBC连接配置上的两个误区
查看>>
荷兰商业银行使用精益领导力推行改进
查看>>
大规模敏捷之Big Room Planning
查看>>
配置一次,到处运行:将配置与运行时解耦
查看>>
突发热点事件下微博高可用注册中心vintage的设计\u0026实践
查看>>
Elixir 1.3带来新的语言功能、API和改进后的工具
查看>>
用Elm语言降低失败的风险
查看>>
C# 8的Ranges和递归模式
查看>>
让VIM支持Python2 by update-alternatives
查看>>
在centos7中搭建redis集群
查看>>
原生JS封装AJAX请求
查看>>