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 Task <VehicleMakesModel> GetMakes(Expression <Func <Vehicle, bool> > predicate) { List <Tuple <string, VehicleConditions> > dataAll = ((QuantumLogicDbContext)DbContextManager.BuildOrCurrentContext(out bool createdNew)) .Vehicles .Where(predicate) .Select(entity => new Tuple <string, VehicleConditions>(entity.Make, entity.Condition)) #warning distinct should be here but not supported yet //.Distinct(new MakesEqualityComparer()) .ToList(); List <Tuple <string, VehicleConditions, int> > data = new List <Tuple <string, VehicleConditions, int> >(); foreach (var make in dataAll.Distinct(new MakesEqualityComparer())) { data.Add(new Tuple <string, VehicleConditions, int>( make.Item1, make.Item2, dataAll.Where(r => r.Item1 == make.Item1 && r.Item2 == make.Item2).Count())); } if (createdNew) { DbContextManager.DisposeContext(); } return(Task.FromResult( new VehicleMakesModel( data.Where(r => r.Item2 == VehicleConditions.New) .Select(r => new VehicleMakeInfo(r.Item1, r.Item3, VehicleMakesImageManager.GetImageForMake(r.Item1))), data.Where(r => r.Item2 == VehicleConditions.Used) .Select(r => new VehicleMakeInfo(r.Item1, r.Item3, VehicleMakesImageManager.GetImageForMake(r.Item1)))))); }
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 Task <IEnumerable <VehicleYearInfo> > GetYears(Expression <Func <Vehicle, bool> > predicate) { IEnumerable <VehicleYearInfo> data = ((QuantumLogicDbContext)DbContextManager.BuildOrCurrentContext(out bool createdNew)) .Vehicles .Where(predicate) .OrderBy(r => r.Id) .GroupBy(entity => entity.Year) .Select(grouping => new VehicleYearInfo(grouping.Last().Year, grouping.Count())) .ToList(); if (createdNew) { DbContextManager.DisposeContext(); } return(Task.FromResult(data)); }
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(); } } }