Exemplo n.º 1
0
        public virtual IList <T> GetListNoPage(Func <T, bool> where, int limit, params Expression <Func <T, object> >[] navigationProperties)
        {
            List <T> list;

            using (var context = new ChabellyEntities())
            {
                IQueryable <T> dbQuery = context.Set <T>();

                //Apply eager loading
                foreach (Expression <Func <T, object> > navigationProperty in navigationProperties)
                {
                    dbQuery = dbQuery.Include <T, object>(navigationProperty);
                }

                if (limit != 0)
                {
                    dbQuery = dbQuery
                              .AsNoTracking()
                              .Where(where).Take(limit).AsQueryable <T>();
                }
                else
                {
                    dbQuery = dbQuery
                              .AsNoTracking()
                              .Where(where).AsQueryable <T>();
                }

                list = dbQuery
                       .ToList <T>();
            }
            return(list);
        }
Exemplo n.º 2
0
 public virtual void UpdateNoValidate(params T[] items)
 {
     try
     {
         using (var context = new ChabellyEntities())
         {
             context.Configuration.ValidateOnSaveEnabled = false;
             foreach (T item in items)
             {
                 context.Entry(item).State = System.Data.EntityState.Modified;
             }
             context.SaveChanges();
         }
     }
     catch (DbEntityValidationException e)
     {
         foreach (var eve in e.EntityValidationErrors)
         {
             Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                               eve.Entry.Entity.GetType().Name, eve.Entry.State);
             foreach (var ve in eve.ValidationErrors)
             {
                 Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                   ve.PropertyName, ve.ErrorMessage);
             }
         }
         throw;
     }
 }
Exemplo n.º 3
0
        public virtual IList <T> GetAll(params Expression <Func <T, object> >[] navigationProperties)
        {
            List <T> list;

            using (var context = new ChabellyEntities())
            {
                IQueryable <T> dbQuery = context.Set <T>();

                if (navigationProperties != null)
                {
                    dbQuery = navigationProperties.Aggregate(dbQuery, (current, include) => current.Include(include));
                }

                //Apply eager loading
                foreach (Expression <Func <T, object> > navigationProperty in navigationProperties)
                {
                    dbQuery = dbQuery.Include <T, object>(navigationProperty);
                }

                list = dbQuery
                       .AsNoTracking()
                       .ToList <T>();
            }
            return(list);
        }
Exemplo n.º 4
0
        public virtual IList <T> GetList(Func <T, bool> where,
                                         out int TotalPages,
                                         string SortBy   = null,
                                         string SortBy2  = null,
                                         bool ascending  = true,
                                         bool ascending2 = true,
                                         int pageIndex   = 0,
                                         int pageSize    = 20,
                                         params Expression <Func <T, object> >[] navigationProperties)
        {
            List <T> list;

            using (var context = new ChabellyEntities())
            {
                IQueryable <T> dbQuery = context.Set <T>();

                //Apply eager loading
                foreach (Expression <Func <T, object> > navigationProperty in navigationProperties)
                {
                    dbQuery = dbQuery.Include <T, object>(navigationProperty);
                }

                if (SortBy != null && SortBy2 != null)
                {
                    if (ascending && ascending2)
                    {
                        dbQuery = dbQuery.AsNoTracking().Where(where).OrderBy(chabelly.Helpers.Helpers.GetExpression <T>(SortBy)).ThenBy(chabelly.Helpers.Helpers.GetExpression <T>(SortBy2))
                                  .AsQueryable <T>().Paged <T>(pageIndex, pageSize, out TotalPages);
                    }
                    else if (ascending && !ascending2)
                    {
                        dbQuery = dbQuery.AsNoTracking().Where(where).OrderBy(chabelly.Helpers.Helpers.GetExpression <T>(SortBy)).ThenByDescending(chabelly.Helpers.Helpers.GetExpression <T>(SortBy2))
                                  .AsQueryable <T>().Paged <T>(pageIndex, pageSize, out TotalPages);
                    }
                    else if (!ascending && ascending2)
                    {
                        dbQuery = dbQuery.AsNoTracking().Where(where).OrderByDescending(chabelly.Helpers.Helpers.GetExpression <T>(SortBy)).ThenBy(chabelly.Helpers.Helpers.GetExpression <T>(SortBy2))
                                  .AsQueryable <T>().Paged <T>(pageIndex, pageSize, out TotalPages);
                    }
                    else
                    {
                        dbQuery = dbQuery.AsNoTracking().Where(where).OrderByDescending(chabelly.Helpers.Helpers.GetExpression <T>(SortBy)).ThenByDescending(chabelly.Helpers.Helpers.GetExpression <T>(SortBy2))
                                  .AsQueryable <T>().Paged <T>(pageIndex, pageSize, out TotalPages);
                    }
                }
                else // Sem Order By(without orderby)
                {
                    dbQuery = dbQuery
                              .AsNoTracking()
                              .Where(where)
                              .AsQueryable <T>().Paged <T>(pageIndex, pageSize, out TotalPages);
                }

                list = dbQuery
                       .ToList <T>();
            }
            return(list);
        }
Exemplo n.º 5
0
 public virtual void Remove(params T[] items)
 {
     using (var context = new ChabellyEntities())
     {
         foreach (T item in items)
         {
             context.Entry(item).State = System.Data.EntityState.Deleted;
         }
         context.SaveChanges();
     }
 }
Exemplo n.º 6
0
        public void RemoveGrupoPermissao(long grupoId, long permissaoId)
        {
            string sql =
                string.Format(@"delete from grupo_permissao where grupo_id = {0} and permissao_id = {1}", grupoId, permissaoId);


            using (var context = new ChabellyEntities())
            {
                context.Database.ExecuteSqlCommand(sql);
            }
        }
Exemplo n.º 7
0
        public virtual void AddNoValidate(params T[] items)
        {
            using (var context = new ChabellyEntities())
            {
                context.Configuration.ValidateOnSaveEnabled = false;
                foreach (T item in items)
                {
                    context.Entry(item).State = System.Data.EntityState.Added;
                }

                context.SaveChanges();
            }
        }
Exemplo n.º 8
0
        public virtual T GetSingle(Func <T, bool> where,
                                   params Expression <Func <T, object> >[] navigationProperties)
        {
            T item = null;

            using (var context = new ChabellyEntities())
            {
                IQueryable <T> dbQuery = context.Set <T>();

                //Apply eager loading
                foreach (Expression <Func <T, object> > navigationProperty in navigationProperties)
                {
                    dbQuery = dbQuery.Include <T, object>(navigationProperty);
                }

                item = dbQuery
                       .AsNoTracking()
                       .FirstOrDefault(where); //Apply where clause
            }
            return(item);
        }