我有一个基于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
并且有时会返回对象,该怎么可能?
我认为这与“锁定”有关,我必须把它放在异步的地方?