コード例 #1
0
ファイル: EFRepositoryX.cs プロジェクト: vipdrv/vipdrv-core
        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);
        }
コード例 #2
0
        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();
                }
            }
        }
コード例 #3
0
        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();
                }
            }
        }
コード例 #4
0
ファイル: UserRepository.cs プロジェクト: vipdrv/vipdrv-core
        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();
                }
            }
        }
コード例 #5
0
ファイル: LeadRepository.cs プロジェクト: vipdrv/vipdrv-core
        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();
                }
            }
        }
コード例 #6
0
 public QuantumLogicUnitOfWork(DbContextManager contextManager, bool useTransaction, Action <QuantumLogicUnitOfWork> disposeAction)
 {
     _contextManager = contextManager;
     _disposeAction  = disposeAction;
     _contextManager.BuildOrCurrentContext();
     if (useTransaction)
     {
         _currentTransaction = _contextManager.CurrentContext.Database.BeginTransaction();
     }
 }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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();
                }
            }
        }
コード例 #10
0
        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();
                }
            }
        }
コード例 #11
0
        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();
                }
            }
        }