首页 > 解决方案 > 使用 NOLOCK 的实体框架查询

问题描述

这个问题被问了很多。我发现这个带有 NOLOCK 的实体框架

当我测试它时,我注意到带有连接的查询

设置事务隔离级别读提交

貌似解决方案只对Single Table Query有效,如何标记Multiple Table Join Query

设置事务隔离级别读未提交

以下是我的测试代码:

class Program
    {
        static void Main(string[] args)
        {
            TransactionOptions transactionOptions = new TransactionOptions();
            transactionOptions.IsolationLevel = IsolationLevel.ReadUncommitted;

            using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
            {

                using (var context = new BloggingContext())
                {                    
                    (from a in context.Set<Post>()
                     join b in context.Set<Blog>() on a.BlogId equals b.BlogId
                     select new { a, b }).FirstOrDefault();

                    context.Set<Blog>().FirstOrDefault();
                    context.Set<Blog>().ToArray();

                    scope.Complete();
                }
            }          

标签: entity-framework

解决方案


推荐阅读