Ejemplo n.º 1
0
        /// <summary>
        ///     提交当前单元操作的结果
        /// </summary>
        /// <returns></returns>
        public int Commit()
        {
            if (IsCommitted)
            {
                return(0);
            }
            try
            {
#if DEBUG
                int result = 0;
                try
                {
                    result = Context.SaveChanges();
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException ex)
                {
                    PublicHelper.ThrowDataAccessException(ex.Message, ex);
                }
#else
                int result = Context.SaveChanges();
#endif
                IsCommitted = true;
                return(result);
            }
            catch (DbUpdateException e)
            {
                if (e.InnerException != null && e.InnerException.InnerException is SqlException)
                {
                    SqlException sqlEx = e.InnerException.InnerException as SqlException;
                    string       msg   = DataHelper.GetSqlExceptionMessage(sqlEx.Number);
                    throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + msg, sqlEx);
                }
                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 提交当前单元操作的结果
        /// </summary>
        /// <returns></returns>
        public async Task <int> CommitAsync(bool validateOnSaveEnabled = true)
        {
            if (IsCommitted)
            {
                return(0);
            }

            try
            {
                int result = await Context.SaveChangesAsync(validateOnSaveEnabled);

                IsCommitted = true;
                return(result);
            }
            catch (DbUpdateException e)
            {
                if (e.InnerException?.InnerException is SqlException)
                {
                    SqlException sqlEx = e.InnerException.InnerException as SqlException;
                    string       msg   = SqlExceptionHelper.GetSqlExceptionMessage(sqlEx.Number);
                    throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + msg, sqlEx);
                }
                throw;
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 ///     提交当前单元操作的结果
 /// </summary>
 /// <param name="validateOnSaveEnabled">保存时是否自动验证跟踪实体</param>
 /// <returns></returns>
 public int Commit(bool validateOnSaveEnabled = true)
 {
     if (IsCommitted)
     {
         return(0);
     }
     try
     {
         int result = Context.SaveChanges(validateOnSaveEnabled);
         IsCommitted = true;
         return(result);
     }
     catch (OptimisticConcurrencyException)
     {
         throw PublicHelper.ThrowDataAccessException("提交数据更新时发现数据已更新,刷新后在进行操作!");
     }
     catch (DbUpdateException e)
     {
         if (e.InnerException != null && e.InnerException.InnerException is SqlException)
         {
             SqlException sqlEx = e.InnerException.InnerException as SqlException;
             string       msg   = DataHelper.GetSqlExceptionMessage(sqlEx.Number);
             throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + msg, sqlEx);
         }
         throw;
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 ///     提交当前单元操作的结果
 /// </summary>
 /// <returns></returns>
 public int Commit()
 {
     if (IsCommitted)
     {
         return(0);
     }
     try
     {
         int result = Context.SaveChanges();
         IsCommitted = true;
         return(result);
     }
     catch (Exception e)
     {
         if (e.InnerException != null && e.InnerException.InnerException is SqlException)
         {
             SqlException sqlEx = e.InnerException.InnerException as SqlException;
             string       msg   = DataHelper.GetSqlExceptionMessage(sqlEx.Number);
             throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + msg, sqlEx);
         }
         if (e.InnerException != null && e.InnerException is OptimisticConcurrencyException)
         {
             throw PublicHelper.ThrowDataAccessException("提交数据更新时发生同步异常:", e.InnerException);
         }
         throw;
     }
 }
Ejemplo n.º 5
0
        public static int SaveChanges(this DbContext dbContext, bool validateOnSaveEnabled)
        {
            bool isReturn = dbContext.Configuration.ValidateOnSaveEnabled != validateOnSaveEnabled;

            try
            {
                dbContext.Configuration.ValidateOnSaveEnabled = validateOnSaveEnabled;
                return(dbContext.SaveChanges());
            }
            catch (DbUpdateException e)
            {
                if (e.InnerException != null && e.InnerException.InnerException is SqlException)
                {
                    SqlException sqlEx = e.InnerException.InnerException as SqlException;
                    string       msg   = DataHelper.GetSqlExceptionMessage(sqlEx.Number);
                    throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + msg, sqlEx);
                }
                throw;
            }
            finally
            {
                if (isReturn)
                {
                    dbContext.Configuration.ValidateOnSaveEnabled = !validateOnSaveEnabled;
                }
            }
        }
Ejemplo n.º 6
0
 public int Commit()
 {
     if (this.IsCommitted)
     {
         return(0);
     }
     try
     {
         int result = this.Context.SaveChanges();
         this.IsCommitted = true;
         return(result);
     }
     catch (DbEntityValidationException validEx)
     {
         StringBuilder sberrors2 = new StringBuilder();
         foreach (DbEntityValidationResult entityValidationError in validEx.EntityValidationErrors)
         {
             sberrors2.Append(string.Format("提交数据实体{0}检验错误:", entityValidationError.Entry.Entity));
             foreach (DbValidationError validationError in entityValidationError.ValidationErrors)
             {
                 sberrors2.Append(string.Format("数据校验错误Property:{0},{1}。", validationError.PropertyName, validationError.ErrorMessage));
             }
         }
         throw PublicHelper.ThrowDataAccessException("提交数据验证时发生异常:" + sberrors2, validEx);
     }
     catch (DbUpdateConcurrencyException deConex)
     {
         DbEntityEntry entry = deConex.Entries.Single();
         entry.OriginalValues.SetValues(entry.GetDatabaseValues());
         throw PublicHelper.ThrowDataAccessException("提交数据并发时发生异常:" + deConex.Message, deConex);
     }
     catch (DbUpdateException dbupEx)
     {
         if (dbupEx.InnerException != null && dbupEx.InnerException.InnerException is SqlException)
         {
             StringBuilder sberrors2 = new StringBuilder();
             SqlException  sqlEx     = dbupEx.InnerException.InnerException as SqlException;
             foreach (SqlError error in sqlEx.Errors)
             {
                 string msg2 = (!string.IsNullOrEmpty(DataHelper.GetSqlExceptionMessage(sqlEx.Number))) ? DataHelper.GetSqlExceptionMessage(sqlEx.Number) : error.Message;
                 sberrors2.Append("SQL Error: " + error.Number + ", Message: " + msg2 + Environment.NewLine);
             }
             throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + sberrors2, sqlEx);
         }
         throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + dbupEx.Message, dbupEx);
     }
     catch (SqlException sqlex)
     {
         StringBuilder sberrors2 = new StringBuilder();
         foreach (SqlError error2 in sqlex.Errors)
         {
             string msg2 = (!string.IsNullOrEmpty(DataHelper.GetSqlExceptionMessage(sqlex.Number))) ? DataHelper.GetSqlExceptionMessage(sqlex.Number) : error2.Message;
             sberrors2.Append("SQL Error: " + error2.Number + ", Message: " + msg2 + Environment.NewLine);
         }
         throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + sberrors2.ToString(), sqlex);
     }
 }
Ejemplo n.º 7
0
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove <OneToManyCascadeDeleteConvention>();

            if (EntityMappers == null)
            {
                //return;
                throw PublicHelper.ThrowDataAccessException("实体映射对象个数为0,创建DbContext上下文对象失败。");
            }

            foreach (var mapper in EntityMappers)
            {
                mapper.RegistTo(modelBuilder.Configurations);
            }
        }
Ejemplo n.º 8
0
 public int Commit()
 {
     try
     {
         int result = Context.SaveChanges();
         return(result);
     }
     catch (DbUpdateException e)
     {
         if (e.InnerException != null && e.InnerException.InnerException is SqlException)
         {
             SqlException sqlEx = e.InnerException.InnerException as SqlException;
             string       msg   = DataHelper.GetSqlExceptionMessage(sqlEx.Number);
             throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + msg, sqlEx);
         }
         throw;
     }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// 提交当前单元操作的结果
        /// </summary>
        /// <param name="validateOnSaveEnabled">保存时是否自动验证跟踪实体</param>
        /// <returns></returns>
        public int Commit(bool validateOnSaveEnabled = true)
        {
            if (IsCommitted)
            {
                return(0);
            }
            try
            {
                int result = Context.SaveChanges(validateOnSaveEnabled);
                IsCommitted = true;
                return(result);
            }
            catch (DbUpdateException ex)
            {
                //todo 用来解决在保存遇到非法实体后无法继续保存的bug 2016/2/18
                var exception = ex.InnerException as UpdateException;
                if (exception != null)
                {
                    var entries = exception.StateEntries.ToList();
                    foreach (var entry in entries)
                    {
                        var entity = entry.Entity;
                        if (entity != null && (EntityState.Added | EntityState.Detached).HasFlag(entry.State) == false)
                        {
                            Context.Entry(entity).Reload();
                        }
                        else
                        {
                            entry.ChangeState(EntityState.Detached);
                        }
                    }
                }

                if (ex.InnerException != null && ex.InnerException.InnerException is SqlException)
                {
                    SqlException sqlEx = ex.InnerException.InnerException as SqlException;
                    string       msg   = DataHelper.GetSqlExceptionMessage(sqlEx.Number);
                    throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + msg, sqlEx);
                }
                throw;
            }
        }
Ejemplo n.º 10
0
 public OperationResult Save()
 {
     try
     {
         if (context.SaveChanges() > 0)
         {
             return(new OperationResult(OperationResultType.Success, "操作成功。"));
         }
         return(new OperationResult(OperationResultType.NoChanged, "未发生任何改变。"));
     }
     catch (DbUpdateException e)
     {
         if (e.InnerException != null && e.InnerException.InnerException is SqlException)
         {
             SqlException sqlEx = e.InnerException.InnerException as SqlException;
             string       msg   = DataHelper.GetSqlExceptionMessage(sqlEx.Number);
             throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + msg, sqlEx);
         }
         throw;
     }
 }
Ejemplo n.º 11
0
 /// <summary>
 /// 提交当前单元操作的结果
 /// </summary>
 /// <param name="validateOnSaveEnabled">保存时是否自动验证跟踪实体</param>
 /// <returns></returns>
 public int Commit(bool validateOnSaveEnabled = true)
 {
     if (IsCommitted)
     {
         return(0);
     }
     try
     {
         int result = Context.SaveChanges(validateOnSaveEnabled);
         IsCommitted = true;
         return(result);
     }
     catch (DbEntityValidationException ex)
     {
         var entry = ex.EntityValidationErrors.First().Entry;
         var err   = ex.EntityValidationErrors.First().ValidationErrors.First();
         var msg   = err.ErrorMessage;
         try
         {
             var displayName = entry.Entity.GetType().GetProperty(err.PropertyName).GetPropertyDisplayName();
             msg = string.Format(msg, displayName, entry.CurrentValues.GetValue <object>(err.PropertyName));
         }
         catch (Exception)
         {
         }
         throw new Exception(msg);
     }
     catch (DbUpdateException e)
     {
         if (e.InnerException != null && e.InnerException.InnerException is SqlException)
         {
             SqlException sqlEx = e.InnerException.InnerException as SqlException;
             string       msg   = DataHelper.GetSqlExceptionMessage(sqlEx.Number);
             throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常:" + msg, sqlEx);
         }
         throw;
     }
 }
        /// <summary>
        ///     提交当前单元操作的结果
        /// </summary>
        /// <returns></returns>
        public int Commit()
        {
            if (IsCommitted)
            {
                return(0);
            }
            try
            {
                int result = Context.SaveChanges();
                IsCommitted = true;
                return(result);
            }
            catch (Exception ex)
            {
                var entities = from entity in Context.ChangeTracker.Entries()
                               where entity.State == EntityState.Added ||
                               entity.State == EntityState.Modified ||
                               entity.State == EntityState.Deleted
                               select entity.Entity;

                var validationResults = new List <ValidationResult>();
                foreach (var entity in entities)
                {
                    var validationContext = new ValidationContext(entity);
                    Validator.TryValidateObject(entity, validationContext, validationResults);
                }
                EntityValidationException validationException = new EntityValidationException(validationResults.Select(x => new ValidationException(x, null, null)));

                StringBuilder sbErrors = new StringBuilder();
                foreach (var err in validationException.ValidationErrors)
                {
                    var validationResult = err.ValidationResult;
                    sbErrors.Append("Error: " + validationResult.MemberNames.First() + ", Message: " + validationResult.ErrorMessage + Environment.NewLine);
                }
                throw PublicHelper.ThrowDataAccessException("提交数据验证时发生异常:" + sbErrors, ex);
            }
        }