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

  • 相关阅读:
    Java网络编程
    IDEA maven-3.6.2 Unable to import maven project错误
    Java通过ArrayList的contains(Object o)方法实现去重字符串中的字符
    常用的Unicode值范围
    Java函数(方法)的默认值问题
    [Noip2017]宝藏
    [Noip2017]组合数问题
    [Noip2018]赛道修建
    [Noip2018]旅行(数据加强版)
    [Noip2019]树的重心
  • 原文地址:https://www.cnblogs.com/miralce/p/6598067.html
Copyright © 2011-2022 走看看