实体框架与NOLOCK [英] Entity Framework with NOLOCK

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

问题描述

如何在Entity Framework上使用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天全站免登陆