带有 NOLOCK 的实体框架 [英] Entity Framework with NOLOCK

查看:18
本文介绍了带有 NOLOCK 的实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在实体框架上使用 NOLOCK 函数?XML 是唯一的方法吗?

How can I use the NOLOCK function on Entity Framework? Is XML the only way to do this?

推荐答案

否,但您可以启动事务并设置 读取未提交的隔离级别.这本质上与 NOLOCK 的作用相同,但它不是在每个表的基础上执行此操作,而是针对事务范围内的所有内容执行此操作.

No, but you can start a transaction and set the isolation level to read uncommited. This essentially does the same as NOLOCK, but instead of doing it on a per table basis, it will do it for everything within the scope of the transaction.

如果这听起来像您想要的那样,那么您可以这样做...

If that sounds like what you want, here's how you could go about doing it...

//declare the transaction options
var transactionOptions = new System.Transactions.TransactionOptions();
//set it to read uncommited
transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
//create the transaction scope, passing our options in
using (var transactionScope = new System.Transactions.TransactionScope(
    System.Transactions.TransactionScopeOption.Required, 
    transactionOptions)
)

//declare our context
using (var context = new MyEntityConnection())
{
    //any reads we do here will also read uncomitted data
    //...
    //...
    //don't forget to complete the transaction scope
    transactionScope.Complete();
}

这篇关于带有 NOLOCK 的实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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