实体框架核心:使用NOLOCK读取和选择记录

时间:2020-08-26 18:15:38

标签: c# .net entity-framework .net-core entity-framework-core

如何使用NOLOCK在Entity Framework Core中读取/选择一个? (以避免在OLTP数据库中发生锁定/阻塞/死锁)。 这是一个示例选择查询。

var data= _dbContext.Set<ProductOrder>()
            .Where(c => c.ProductTypeId == this.productTypeId && c.saleYear == this.saleYear)
            .ToList();

将Net Core 3.1与SQL Server 2016数据库一起使用。

1 个答案:

答案 0 :(得分:0)

您可以将NOLOCKEF Core一起使用

using (new TransactionScope(TransactionScopeOption.Required, new TransactionOptions
{
    IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
}))
{
    using (var db = new YourDbContext())
    {
        var data = db.Set<ProductOrder>()
            .Where(c => c.ProductTypeId == this.productTypeId && c.saleYear == this.saleYear)
            .ToList();
    }
}