【问题标题】:Entity framework 4.0 and 5.0 Code First hint IQueryable query with NOLOCK实体框架 4.0 和 5.0 Code First 提示使用 NOLOCK 的 IQueryable 查询
【发布时间】:2013-02-06 20:44:20
【问题描述】:

我使用的是 EF 5.0 Code First。我们无法对每个读取查询实施 nolock。请在下面找到代码。

我的模特:

public class UserType
{
    public UserType()
    {
        CreationDate = DateTime.Now;
        UpdatedDate = DateTime.Now;
        DeletedFlag = false;
    }
    public int UserTypeId { get; set; }
    public string UserTypeName { get; set; }

    public int CreatedBy { get; set; }
    public int UpdatedBy { get; set; }
    public DateTime CreationDate { get; set; }
    public DateTime UpdatedDate { get; set; }
    public bool DeletedFlag { get; set; }
}

RepositoryBase:(我们使用的是存储库设计模式)

 public IQueryable<T> Query<T>() where T : class
    {
            return DataContext.Set<T>();
    }

服务:

//TODO: place NOLOCK
repBase.Query<UserType>().Where(ja => ja.DeletedFlag == false).OrderByDescending(ja => ja.UpdatedDate).ToList();

repBase 是要在 Query() 之上调用的 DB 上下文的实例。

现在我们要使用 (NOLOCK) 运行上述查询。

Visual Studio 2012, EF 5.0 代码优先, C#.Net 4.0, MVC Web API, 存储库设计模式

提前致谢。

【问题讨论】:

  • 您无法使用 EF 发出无锁提示(或任何提示)。但是,只是好奇,你需要它做什么?
  • 为了避免死锁和允许脏读,内部应用会修改数据,但面向公众的网站只是只读的

标签: c#-4.0 visual-studio-2012 ef-code-first entity-framework-5


【解决方案1】:

不支持。 EF 永远不会在查询中发出NOLOCK,除了通过Database.SqlQueryDbSet.SqlQuery 执行直接SQL 之外,没有其他方法可以做到这一点。如果你想让 NOLOCK 由 EF 生成,你可以下载 EF 源代码并修改其 SQL 生成引擎以添加提示。无论如何,您应该三思而后行在全球范围内使用NOLOCK

【讨论】:

  • 为了避免死锁和允许脏读,内部应用会修改数据,但面向公众的网站只是只读的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-27
  • 1970-01-01
相关资源
最近更新 更多