EF 6.1.3(ASP.NET 5 RC1应用程序)异步存储库结果不一致

时间:2016-07-05 14:18:01

标签: c# entity-framework asynchronous asp.net-core asp.net-core-mvc

我有一个基于ASP.NET Core 1.1 RC的应用程序使用异步EF6通用基本存储库,它具有GetAsync方法:

public virtual async Task<T> GetAsync(Expression<Func<T, bool>> predicate, bool includeSoftDeleted = false) 
    {
        try
        {
            return 
                await
                    _context.Set<T>().Where(e => includeSoftDeleted || !e.IsDeleted).FirstOrDefaultAsync(predicate);
        }
        catch (Exception ex)
        {
            _logger.LogError($"Unable to get the {typeof(T).Name}. See the exception for more details.",predicate,ex);
            return null;
        }
    }

我的所有存储库都使用此类作为基类,并在我的服务中注入Autofac作为InstancePerLifetime()

所以我的服务中有一个调用此GetAsync的方法:

 public async Task<int?> GetRoleAccessPermissionLevelAsync(UserRole userRole, Access access)
    {
        // TODO: Implement IEquatable for Access to replace with a == access
        var accessFromDb = await _accessRepository.GetAsync(a => a.Module == access.Module && a.Controller == access.Controller && a.Action == access.Action);

        if(accessFromDb == null) return null;

        var roleAccess = await _roleAccessRepository.GetAsync(r =>
           r.RoleId == userRole.RoleId && r.AccessId == accessFromDb.Id);

        return (int?)roleAccess?.AccessLevel;
    }

此处,accessFromDb有时为null,有时不会,给出相同的值:access.Module = "Main",控制器和操作为null

如果不更改context或存储库的Set<T>(),有时可能null并且有时会返回对象,该怎么可能?

我认为这与“锁定”有关,我必须把它放在异步的地方?

0 个答案:

没有答案