/// <summary> /// Delete current entity /// </summary> /// <param name="entity">TEntity</param> /// <returns>int</returns> public int Delete(params TEntity[] entitys) { using (var context = new DocumentDBEntities()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { context.Set <TEntity>().RemoveRange(entitys); int result = context.SaveChanges(); dbContextTransaction.Commit(); return(result); } catch (DbEntityValidationException dbex) { log.ErrorFormat("==> Delete entity is error :", entitys.GetType().Name); log.ErrorFormat("Error is unexpected message=[{0}],\n InnerException=[{1}],\n StackTrace=[{2}]", dbex.Message, dbex.InnerException, dbex.StackTrace); dbContextTransaction.Rollback(); return(-2); } catch (Exception ex) { log.ErrorFormat("==> Delete entity is error :", entitys.GetType().Name); log.ErrorFormat("Error is unexpected message=[{0}],\n InnerException=[{1}],\n StackTrace=[{2}]", ex.Message, ex.InnerException, ex.StackTrace); dbContextTransaction.Rollback(); return(-2); } } } }
/// <summary> /// Get list entity /// </summary> /// <returns>List<TEntity></returns> public IEnumerable <TEntity> Select() { try { var context = new DocumentDBEntities(); return(context.Set <TEntity>().AsNoTracking().ToList <TEntity>()); } catch (Exception ex) { log.ErrorFormat("==> Get all data [{0}] is error", typeof(TEntity).Name); log.ErrorFormat("Error is unexpected message=[{0}],\n InnerException=[{1}],\n StackTrace=[{2}]", ex.Message, ex.InnerException, ex.StackTrace); return(null); } }
/// <summary> /// Get list entity by function and predicate /// </summary> /// <param name="predicate">Expression<Func<TEntity, bool>></param> /// <returns>List<TEntity></returns> public IEnumerable <TEntity> Select(Expression <Func <TEntity, bool> > predicate) { try { var context = new DocumentDBEntities(); List <TEntity> result = context.Set <TEntity>().AsNoTracking().Where(predicate).ToList <TEntity>(); return(result); } catch (Exception ex) { string paramsList = string.Empty; log.ErrorFormat("==> Get all data [{0}] by where is error", predicate.GetType().Name); log.ErrorFormat("==> Where clause =[{0}]", predicate.Body); log.ErrorFormat("Error is unexpected message=[{0}],\n InnerException=[{1}],\n StackTrace=[{2}]", ex.Message, ex.InnerException, ex.StackTrace); return(null); } }
/// <summary> /// Update value for entity /// </summary> /// <param name="entitys">TEntity</param> /// <returns>int</returns> public int Update(params TEntity[] entitys) { using (var context = new DocumentDBEntities()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { foreach (var updateItem in entitys) { var update = context.Set <TEntity>().Add(updateItem); context.Entry <TEntity>(updateItem).State = System.Data.Entity.EntityState.Modified; } int result = context.SaveChanges(); dbContextTransaction.Commit(); return(result); } catch (DbUpdateConcurrencyException dbucex) { log.ErrorFormat("==> Update entity is error", entitys.GetType().Name); log.ErrorFormat("Error is unexpected message=[{0}],\n InnerException=[{1}],\n StackTrace=[{2}]", dbucex.Message, dbucex.InnerException, dbucex.StackTrace); dbContextTransaction.Rollback(); return(-2); } catch (System.Data.Entity.Validation.DbEntityValidationException dbevex) { log.ErrorFormat("==> Update entity is error", entitys.GetType().Name); log.ErrorFormat("Error is unexpected message=[{0}],\n InnerException=[{1}],\n StackTrace=[{2}]", dbevex.Message, dbevex.InnerException, dbevex.StackTrace); dbContextTransaction.Rollback(); return(-2); } catch (Exception ex) { log.ErrorFormat("==> Update entity is error", entitys.GetType().Name); log.ErrorFormat("Error is unexpected message=[{0}],\n InnerException=[{1}],\n StackTrace=[{2}]", ex.Message, ex.InnerException, ex.StackTrace); dbContextTransaction.Rollback(); return(-2); } } } }