//update public virtual async Task RewriteSets(long subscriberId, List <SubscriberScheduleSettings <long> > periods) { foreach (SubscriberScheduleSettings <long> item in periods) { item.PeriodBegin = SqlDataFomatting.ToSqlTime(item.PeriodBegin); item.PeriodEnd = SqlDataFomatting.ToSqlTime(item.PeriodEnd); } List <int> sets = periods .Select(x => x.Set) .Distinct() .ToList(); List <SubscriberScheduleSettingsLong> periodsMapped = periods .Select(_mapper.Map <SubscriberScheduleSettingsLong>) .ToList(); using (var repository = new Repository(_dbContextFactory.GetDbContext())) using (IDbContextTransaction ts = repository.Context.Database.BeginTransaction()) { SqlTransaction underlyingTransaction = (SqlTransaction)ts.GetDbTransaction(); int changes = await repository.DeleteManyAsync <SubscriberScheduleSettingsLong>( x => x.SubscriberId == subscriberId && sets.Contains(x.Set)) .ConfigureAwait(false); InsertCommand <SubscriberScheduleSettingsLong> insertCommand = repository .Insert <SubscriberScheduleSettingsLong>(underlyingTransaction); insertCommand.Insert .ExcludeProperty(x => x.SubscriberScheduleSettingsId); changes = await insertCommand.ExecuteAsync(periodsMapped).ConfigureAwait(false); ts.Commit(); } }
/// <summary> /// Construct a query to select page of rows /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TOrder"></typeparam> /// <param name="pageIndex">0-based page index</param> /// <param name="pageSize"></param> /// <param name="descending"></param> /// <param name="whereExpression"></param> /// <param name="orderExpression"></param> /// <returns></returns> public virtual IQueryable <TEntity> FindPageQuery <TEntity, TOrder>(int pageIndex, int pageSize, bool descending , Expression <Func <TEntity, bool> > whereExpression, Expression <Func <TEntity, TOrder> > orderExpression) where TEntity : class { int skip = SqlDataFomatting.ToSkipNumberZeroBased(pageIndex, pageSize); IQueryable <TEntity> query = Context.Set <TEntity>().AsQueryable(); if (whereExpression != null) { query = query.Where(whereExpression); } if (descending) { query = query.OrderByDescending(orderExpression); } else { query = query.OrderBy(orderExpression); } if (skip > 0) { query = query.Skip(skip); } query = query.Take(pageSize); return(query); }
//insert public virtual async Task Insert(List <SubscriberScheduleSettings <long> > periods) { foreach (SubscriberScheduleSettings <long> item in periods) { item.PeriodBegin = SqlDataFomatting.ToSqlTime(item.PeriodBegin); item.PeriodEnd = SqlDataFomatting.ToSqlTime(item.PeriodEnd); } List <SubscriberScheduleSettingsLong> periodsMapped = periods .Select(_mapper.Map <SubscriberScheduleSettingsLong>) .ToList(); using (Repository repository = new Repository(_dbContextFactory.GetDbContext())) { InsertCommand <SubscriberScheduleSettingsLong> command = repository.Insert <SubscriberScheduleSettingsLong>(); command.Insert.ExcludeProperty(x => x.SubscriberScheduleSettingsId); int changes = await command.ExecuteAsync(periodsMapped).ConfigureAwait(false); } }