码迷,mamicode.com
首页 > 其他好文 > 详细

在EntityFramework中使用 nock的方法。

时间:2016-06-22 10:42:51      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

以下内容为转载:

A:https://dotblogs.com.tw/asdtey/2009/09/27/10793

B:http://www.gitshah.com/2014/08/how-to-add-nolock-hint-to.html

 

1,方法一   使用 TransactionScope

2,  使用 ObjectContext.Connection.BeginTransaction

  

using (TestEntities te = new TestEntities()) {

////方法二 ////此方法會修改所有操作的交易層級 te.Connection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);

var users = te.User.Select(a => a).ToList();

}

 

3,重写 DbCommandInterceptor

public class NoLockInterceptor : DbCommandInterceptor

{

private static readonly Regex _tableAliasRegex =

new Regex(@"(?<tablealias>AS \[Extent\d+\](?! WITH \(NOLOCK\)))",

RegexOptions.Multiline | RegexOptions.IgnoreCase);

[ThreadStatic]

public static bool ApplyNoLock;

public override void ScalarExecuting(DbCommand command,

DbCommandInterceptionContext<object> interceptionContext)

{

if (ApplyNoLock)

{

command.CommandText =

_tableAliasRegex.Replace(command.CommandText,

"${tableAlias} WITH (NOLOCK)");

}

}

public override void ReaderExecuting(DbCommand command,

DbCommandInterceptionContext<dbdatareader> interceptionContext)

{

if (ApplyNoLock)

{

command.CommandText =

_tableAliasRegex.Replace(command.CommandText,

"${tableAlias} WITH (NOLOCK)");

}

}

}

3.1

NoLockInterceptor.ApplyNoLock = true;

3.2

DbInterception.Add(new NoLockInterceptor());

在EntityFramework中使用 nock的方法。

标签:

原文地址:http://www.cnblogs.com/zbw911/p/5605996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!