zoukankan      html  css  js  c++  java
  • Entity Framework with NOLOCK

    Ef 使用类似sqlserver 的nolock 查询扩展类

        /// <summary>
        /// 类似SqlServer nolock 查询扩展
        /// Like SqlServer Nolock
        /// </summary>
        public static class NoLockQuery
        {
            public static List<T> ToListReadUncommitted<T>(this IQueryable<T> query)
            {
                using (var scope = new TransactionScope(
                    TransactionScopeOption.Required,
                    new TransactionOptions()
                    {
                        IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
                    }))
                {
                    List<T> toReturn = query.ToList();
                    scope.Complete();
                    return toReturn;
                }
            }
    
            public static int CountReadUncommitted<T>(this IQueryable<T> query)
            {
                using (var scope = new TransactionScope(
                    TransactionScopeOption.Required,
                    new TransactionOptions()
                    {
                        IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
                    }))
                {
                    int toReturn = query.Count();
                    scope.Complete();
                    return toReturn;
                }
            }
    
            public static async Task<List<T>> ToListReadUncommittedAsync<T>(this IQueryable<T> query)
            {
                using (var scope = new TransactionScope(
                    TransactionScopeOption.Required,
                    new TransactionOptions()
                    {
                        IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
                    },
                      TransactionScopeAsyncFlowOption.Enabled
                    ))
                {
                    List<T> toReturn = await query.ToListAsync();
                    scope.Complete();
                    return toReturn;
                }
            }
    
            public static async Task<int> CountReadUncommittedAsync<T>(this IQueryable<T> query)
            {
                using (var scope = new TransactionScope(
                    TransactionScopeOption.Required,
                    new TransactionOptions()
                    {
                        IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
                    },
                     TransactionScopeAsyncFlowOption.Enabled
                    ))
                {
                    int toReturn = await query.CountAsync();
                    scope.Complete();
                    return toReturn;
                }
            }
        }

    参考:http://stackoverflow.com/questions/926656/entity-framework-with-nolock

  • 相关阅读:
    执行start-dfs.sh后,datenode没有启动的解决办法
    hadoop 在centos中的搭建
    MySQL 5.7的安装及主从复制(主从同步)
    CentOS7 配置免密码登陆
    关于使用maven打包如何聚合资源文件
    idea常用快捷键
    lombok的使用
    oracle 导出,导入表
    vue项目.eslintrc格式化
    vue-cli3项目关闭烦人的代码检测
  • 原文地址:https://www.cnblogs.com/miralce/p/6598067.html
Copyright © 2011-2022 走看看