public async Task <int?> GetMaxExistedOrder(Func <IQueryable <TEntity>, IQueryable <TEntity> > queryBuilder, params Expression <Func <TEntity, object> >[] includes) { bool createdNew; DbSet <TEntity> set = DbContextManager.BuildOrCurrentContext(out createdNew).Set <TEntity>(); IQueryable <TEntity> query = OnSystemFilters ? await ApplySystemFilters(queryBuilder(ApplyIncludes(set, includes))) : queryBuilder(ApplyIncludes(set, includes)); int?maxExistedOrder; try { maxExistedOrder = query.Max(r => r.Order); } catch (InvalidOperationException) { // means query was empty maxExistedOrder = null; } IList <TEntity> entities = await query.ToListAsync(); if (createdNew) { DbContextManager.DisposeContext(); } return(maxExistedOrder); }
public virtual async Task <TEntity> GetAsync(TPrimaryKey id, params Expression <Func <TEntity, object> >[] includes) { bool createdNew = false; try { DbSet <TEntity> set = DbContextManager.BuildOrCurrentContext(out createdNew).Set <TEntity>(); IQueryable <TEntity> temp = OnSystemFilters ? await ApplySystemFilters(Queryable.Where(ApplyIncludes(set, includes), CreateEqualityExpressionForId(id))) : Queryable.Where(ApplyIncludes(set, includes), CreateEqualityExpressionForId(id)); TEntity entity = await temp.FirstOrDefaultAsync(); if (entity == null) { throw new EntityNotFoundException($"Entity {typeof(TEntity).Name} with id {id} not found."); } return(entity); } finally { if (createdNew) { DbContextManager.DisposeContext(); } } }
public virtual async Task <TEntity> SingleAsync(Expression <Func <TEntity, bool> > filter, params Expression <Func <TEntity, object> >[] includes) { bool createdNew = false; try { DbSet <TEntity> set = DbContextManager.BuildOrCurrentContext(out createdNew).Set <TEntity>(); IQueryable <TEntity> query = OnSystemFilters ? await ApplySystemFilters(Queryable.Where(ApplyIncludes(set, includes), filter)) : Queryable.Where(ApplyIncludes(set, includes), filter); TEntity entity = null; try { entity = await query.SingleOrDefaultAsync(); } catch (Exception ex) { throw new EntityNotFoundException($"Entity {typeof(TEntity).Name} with predicate {filter.ToString()} is not unique.", ex); } if (entity == null) { throw new EntityNotFoundException($"Entity {typeof(TEntity).Name} with predicate {filter.ToString()} not found."); } return(entity); } finally { if (createdNew) { DbContextManager.DisposeContext(); } } }
public virtual async Task <User> FirstOrDefaultWithDeepIncludesAsync(Expression <Func <User, bool> > filter) { bool createdNew = false; try { DbSet <User> set = DbContextManager.BuildOrCurrentContext(out createdNew).Set <User>(); var query = set .Include(r => r.UserClaims) .ThenInclude(r => r.Claim) .Include(r => r.UserRoles) .ThenInclude(r => r.Role) .ThenInclude(r => r.RoleClaims) .ThenInclude(r => r.Claim) .Include(r => r.ExternalLogins); IQueryable <User> temp = OnSystemFilters ? await ApplySystemFilters(Queryable.Where(query, filter)) : Queryable.Where(query, filter); User entity = await temp.FirstOrDefaultAsync(); return(entity); } finally { if (createdNew) { DbContextManager.DisposeContext(); } } }
public async Task NullifyRouteRelation(Func <IQueryable <Lead>, IQueryable <Lead> > queryBuilder) { bool createdNew = false; try { DbContext context = DbContextManager.BuildOrCurrentContext(out createdNew); List <Lead> leads = EntityFrameworkQueryableExtensions.AsNoTracking(queryBuilder(context.Set <Lead>())).ToList(); foreach (var lead in leads) { Lead stubLead = new Lead() { Id = lead.Id, RouteId = null }; context.Set <Lead>().Attach(stubLead); context.Entry(stubLead).Property(x => x.RouteId).IsModified = true; } } finally { if (createdNew) { await DbContextManager.CurrentContext.SaveChangesAsync(); DbContextManager.DisposeContext(); } } }
public QuantumLogicUnitOfWork(DbContextManager contextManager, bool useTransaction, Action <QuantumLogicUnitOfWork> disposeAction) { _contextManager = contextManager; _disposeAction = disposeAction; _contextManager.BuildOrCurrentContext(); if (useTransaction) { _currentTransaction = _contextManager.CurrentContext.Database.BeginTransaction(); } }
public virtual async Task <int> GetTotalCountAsync(Func <IQueryable <TEntity>, IQueryable <TEntity> > queryBuilder) { bool createdNew; DbSet <TEntity> set = DbContextManager.BuildOrCurrentContext(out createdNew).Set <TEntity>(); IQueryable <TEntity> query = OnSystemFilters ? await ApplySystemFilters(queryBuilder(set)) : queryBuilder(set); int totalCount = query.Count(); if (createdNew) { DbContextManager.DisposeContext(); } return(totalCount); }
public virtual async Task <IList <TEntity> > GetAllAsync(Func <IQueryable <TEntity>, IQueryable <TEntity> > queryBuilder, params Expression <Func <TEntity, object> >[] includes) { bool createdNew; DbSet <TEntity> set = DbContextManager.BuildOrCurrentContext(out createdNew).Set <TEntity>(); IQueryable <TEntity> query = OnSystemFilters ? await ApplySystemFilters(queryBuilder(ApplyIncludes(set, includes))) : queryBuilder(ApplyIncludes(set, includes)); IList <TEntity> entities = await query.ToListAsync(); if (createdNew) { DbContextManager.DisposeContext(); } return(entities); }
public virtual async Task DeleteAsync(TEntity entity) { bool createdNew = false; try { DbContextManager.BuildOrCurrentContext(out createdNew).Remove(entity); } finally { if (createdNew) { await DbContextManager.CurrentContext.SaveChangesAsync(); DbContextManager.DisposeContext(); } } }
public virtual async Task DeleteRange(Func <IQueryable <TEntity>, IQueryable <TEntity> > queryBuilder) { bool createdNew = false; try { var entities = await GetAllAsync(queryBuilder); DbContextManager.BuildOrCurrentContext(out createdNew).RemoveRange(entities); } finally { if (createdNew) { await DbContextManager.CurrentContext.SaveChangesAsync(); DbContextManager.DisposeContext(); } } }
public virtual async Task <TEntity> FirstOrDefaultAsync(Expression <Func <TEntity, bool> > filter, params Expression <Func <TEntity, object> >[] includes) { bool createdNew = false; try { DbSet <TEntity> set = DbContextManager.BuildOrCurrentContext(out createdNew).Set <TEntity>(); IQueryable <TEntity> temp = OnSystemFilters ? await ApplySystemFilters(Queryable.Where(ApplyIncludes(set, includes), filter)) : Queryable.Where(ApplyIncludes(set, includes), filter); TEntity entity = await temp.FirstOrDefaultAsync(); return(entity); } finally { if (createdNew) { DbContextManager.DisposeContext(); } } }