예제 #1
0
        /// <summary>
        /// Get list of Entiy paging
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="page"></param>
        /// <param name="order"></param>
        /// <param name="predicate"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IEnumerable <TEntity> Get <TEntity>(int page, Expression <Func <TEntity, string> > order, Expression <Func <TEntity, bool> > predicate = null, int pageSize = 1000) where TEntity : class
        {
            try
            {
                using (Context = new InFreshContext())
                {
                    int             skipRows = 0;
                    DbSet <TEntity> set      = Context.Set <TEntity>();
                    skipRows = page > 1 ? page * pageSize : 0;

                    var query = set.Where(predicate)
                                .OrderBy(order)
                                .Skip(skipRows).Take(pageSize)
                                .GroupBy(p => new { Total = set.Count() })
                                .FirstOrDefault();

                    return(query.Select(p => p));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return(null);
        }
예제 #2
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="predicate"></param>
 /// <returns></returns>
 public IEnumerable <TEntity> Get <TEntity>(Expression <Func <TEntity, bool> > predicate)
     where TEntity : class
 {
     try
     {
         Context = new InFreshContext();
         DbSet <TEntity> set = Context.Set <TEntity>();
         if (predicate == null)
         {
             return(set.AsEnumerable());
         }
         else
         {
             return(set.Where(predicate).AsEnumerable());
         }
     }
     catch
     {
         return(null);
     }
 }
예제 #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="context"></param>
 public UnitOfWork(InFreshContext context)
 {
     //Context = context;
 }
예제 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public string Delete <TEntity>(TEntity t = null) where TEntity : class
        {
            int            TResult = -1;
            InFreshContext context = null;

            try
            {
                using (context = new InFreshContext())
                {
                    DbSet <TEntity> set = context.Set <TEntity>();
                    //if (context.Entry(t).State == EntityState.Detached)
                    //    set.Attach(t);
                    var obj = set.Remove(set.Find(t));
                    if (obj.Equals(t))
                    {
                        TResult = 0;
                    }
                    else
                    {
                        TResult = 1;
                    }

                    if (TResult == 0)
                    {
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                TResult = -1;
                var oc = ((IObjectContextAdapter)context).ObjectContext;

                var objectStateEntries = oc.ObjectStateManager
                                         .GetObjectStateEntries(EntityState.Added);
                foreach (var objectStateEntry in objectStateEntries)
                {
                    oc.Detach(objectStateEntry.Entity);
                }

                StringBuilder sb = new StringBuilder();
                if (ex.GetType() == typeof(DbEntityValidationException))
                {
                    DbEntityValidationException dbex = ex as DbEntityValidationException;

                    foreach (var failure in dbex.EntityValidationErrors)
                    {
                        sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
                        foreach (var error in failure.ValidationErrors)
                        {
                            sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                            sb.AppendLine();
                        }
                    }

                    //sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);

                    throw new DbEntityValidationException(
                              "Entity Validation Failed - errors follow:\n" +
                              sb.ToString(), ex
                              );
                }
                else
                {
                    if (ex.InnerException.InnerException != null)
                    {
                        sb.Append(ex.InnerException.InnerException.Message);
                    }
                    else
                    {
                        sb.Append(ex.Message);
                    }
                    throw new Exception(sb.ToString(), ex);
                }
            }

            return(TResult.ToString());
        }
예제 #5
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public IEnumerable <TEntity> Get <TEntity>(Expression <Func <TEntity, bool> > predicate = null) where TEntity : class
        {
            IEnumerable <TEntity> data    = null;
            InFreshContext        context = new InFreshContext();

            try
            {
                DbSet <TEntity> set = context.Set <TEntity>();
                if (predicate == null)
                {
                    data = set.AsEnumerable();
                }
                else
                {
                    data = set.Where(predicate).AsEnumerable();
                }
                return(data);
            }
            catch (Exception ex)
            {
                var oc = ((IObjectContextAdapter)context).ObjectContext;

                var objectStateEntries = oc.ObjectStateManager
                                         .GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged);
                foreach (var objectStateEntry in objectStateEntries)
                {
                    oc.Detach(objectStateEntry.Entity);
                }

                StringBuilder sb = new StringBuilder();
                if (ex.GetType() == typeof(DbEntityValidationException))
                {
                    DbEntityValidationException dbex = ex as DbEntityValidationException;

                    foreach (var failure in dbex.EntityValidationErrors)
                    {
                        sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
                        foreach (var error in failure.ValidationErrors)
                        {
                            sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                            sb.AppendLine();
                        }
                    }

                    //sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);

                    throw new DbEntityValidationException(
                              "Entity Validation Failed - errors follow:\n" +
                              sb.ToString(), ex
                              );
                }
                else
                {
                    if (ex.InnerException.InnerException != null)
                    {
                        sb.Append(ex.InnerException.InnerException.Message);
                    }
                    else
                    {
                        sb.Append(ex.Message);
                    }
                    throw new Exception(sb.ToString(), ex);
                }
            }
            finally
            {
                //context.Dispose();
                context = null;
                //GC.SuppressFinalize(this);
            }
            //return null;
        }