实体框架中的NoLock

本文关键字:NoLock 框架 实体 | 更新日期: 2023-09-27 18:15:06

我试图从表中读取记录,即使表由于特定事务而被锁定。

我使用下面的代码

public async Task<KeyValuePair<String, List<Om_Category>>> CategoryList(Om_Category obj)
{
    try
    {
        using (var transaction = new TransactionScope(
                TransactionScopeOption.Required,
                new TransactionOptions 
                { 
                    IsolationLevel = IsolationLevel.ReadUncommitted 
                },
                TransactionScopeAsyncFlowOption.Enabled))
        {
            using (var categoryContext = new ModelGeneration())
            {
                categoryContext.Configuration.ProxyCreationEnabled = false;
                var data = await categoryContext
                    .tblCategory
                    .ToListAsync();
                transaction.Complete();
                return new KeyValuePair<String, List<Om_Category>>("", data);
            }
        }
    }
    catch (Exception ex)
    {
        return new KeyValuePair<String, List<Om_Category>>(ex.Message, null);
    }
}

但是似乎我缺少一些东西来实现nollocks。还是它显示超时。我错过什么了吗?

实体框架中的NoLock

令人惊讶的实体框架内的事务类工作!!

public async Task<KeyValuePair<String, List<BE_Category>>> CategoryList(BE_Category obj)
{
    try
    {
        using (var categoryContext = new ModelGeneration())
        {
            using (var dbContextTransaction = categoryContext
                      .Database
                      .BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
            {
                categoryContext.Configuration.ProxyCreationEnabled = false;
                //Code
                dbContextTransaction.Commit();
                return new KeyValuePair<String, List<BE_Category>>("", data);
            }
        }
    }
    catch (Exception ex)
    {
        return new KeyValuePair<String, List<BE_Category>>(ex.Message, null);
    }
}
参考