NoLock on Entity Framework

I try to read records from a table, even when the table is locked due to a specific transaction.

I am using the code below

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);
    }
}

But it seems like I'm missing something to implement NoLocks. Still show a timeout. Did I miss something?

+4
source share
1 answer

It's amazing that the Entity Framework built-in transaction class works!

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);
    }
}

Link

+2
source

Source: https://habr.com/ru/post/1609557/


All Articles