コード例 #1
0
 protected override ExecuteResult <int> DoCommit(int retry = 0)
 {
     if (Committed)
     {
         return(ExecuteResult <int> .CreateFaile(0, "已提交"));
     }
     try
     {
         var result = this.Context.SaveChanges();
         this.Committed = true;
         return(ExecuteResult <int> .CreateSuccess(result, "提交成功"));
     }
     catch (Exception ex)
     {
         YmatouLoggingService.Warning("执行失败 {0},重试 {1} 次", ex.ToString(), retry);
         //重试
         //return RetryUtility.RetryAction2(() =>
         // {
         //     var result = this.Context.SaveChanges();
         //     this.Committed = true;
         //     return Tuple.Create<ExecuteResult<int>, bool>(new ExecuteResult<int>(true, null, result), result > 0);
         // }, retry, 3000).Item1;
         //RetryUtility.RetryAction2(() => System.Tuple.Create<ExecuteResult<int>, bool>(new ExecuteResult<int>(true, null, 0), true), 1000);
         return(new ExecuteResult <int>(true, null, 0));
     }
 }
コード例 #2
0
        protected override ExecuteResult <Task <int> > DoAsyncCommit(int retry = 0)
        {
            if (Committed)
            {
                return(ExecuteResult <Task <int> > .CreateFaile(Task.Factory.StartNew(() => 0), "已提交"));
            }
            try
            {
                var result = this.Context.SaveChangesAsync();
                Committed = true;
                return(ExecuteResult <Task <int> > .CreateSuccess(result, "提交成功"));
            }
            catch (Exception ex)
            {
                ExecuteResult <bool> .CreateFaile(false, ex.Message);

                return(ExecuteResult <Task <int> > .CreateFaile(Task.Factory.StartNew(() => 0), ex.Message));
            }
        }
コード例 #3
0
ファイル: EFUnitOfWork.cs プロジェクト: vebin/WorkSystem
        public ExecuteResult <int> Commit(int retry = 0, bool lockd = false)
        {
            var tmpRetry = retry;
            var fail     = false;
            var result   = 0;

            do
            {
                try
                {
                    result = base.SaveChanges();
                    fail   = false;
                    YmatouLoggingService.Debug("Db Context Commit {0}", result);
                    return(ExecuteResult <int> .CreateSuccess(result, "提交成功"));
                }
                catch (DbEntityValidationException ex)
                {
                    ex.EntityValidationErrors.Each(_e =>
                    {
                        var errs = _e.Entry.GetValidationResult().ValidationErrors.ValidationErrorAppendToString();
                        YmatouLoggingService.Error("entity:{0},Validation fail:{1}", _e.Entry.Entity.GetType().Name,
                                                   errs);
                    });
                    return(ExecuteResult <int> .CreateFaile(0, "Db Context Commit fail"));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    fail = true;
                    ex.Entries
                    .Each(entry =>
                    {
                        if (entry != null)
                        {
                            if (entry.State == EntityState.Added || entry.State == EntityState.Modified)
                            {
                                //Context中实体的值覆盖db中实体的值
                                var currentVal = entry.CurrentValues;     //当前值
                                var dbVal      = entry.GetDatabaseValues();
                                //获取数据库中的值
                                entry.OriginalValues.SetValues(dbVal);
                                //entry.CurrentValues.SetValues(dbVal.Clone());
                                entry.State = EntityState.Modified;
                            }
                            else
                            {
                                //数据库中的覆盖entity中的值
                                entry.Reload();
                            }
                        }
                    }, errorHandler: err =>
                    {
                        YmatouLoggingService.Error("重试异常 {0}", err.ToString());
                    });
                    var _ex = ex.GetBaseException() as SqlException;
                    if (_ex != null && (_ex.Number == 2627 || _ex.Number == 515))
                    {
                        YmatouLoggingService.Debug("EFUnitOfWork DbUpdateConcurrencyException repeat insert,{0}",
                                                   _ex.Message);
                    }
                    else
                    {
                        YmatouLoggingService.Error(
                            "EFUnitOfWork DbUpdateConcurrencyException entity : {0},err:{1},重试 {2} 次",
                            ex.Entries.TrySerializeEntity(),
                            ex.ToString(), tmpRetry);
                    }
                }
//                catch (DbUpdateException ex)
//                {
//                    fail = true;
//                    result = -1;
//                    var _ex = ex.GetBaseException() as SqlException;
//                    if (_ex != null && (_ex.Number == 2627 || _ex.Number == 515))
//                        YmatouLoggingService.Debug("EFUnitOfWork DbUpdateException repeat insert,{0}", _ex.Message);
//                    else
//                        YmatouLoggingService.Error("EFUnitOfWork DbUpdateException entity : {0},err:{1},重试 {2} 次",
//                            ex.Entries.TrySerializeEntity(),
//                            ex.ToString(), tmpRetry);
//                    throw;
//                }
            } while (fail && tmpRetry-- > 0);
            return(new ExecuteResult <int>(fail == false, fail == false ? "执行成功" : "更新失败", result));
        }
コード例 #4
0
ファイル: EFUnitOfWork.cs プロジェクト: vebin/WorkSystem
        public async Task <ExecuteResult <int> > AsyncCommit(int retry = 0)
        {
            var  tmpRetry = retry;
            bool fail;
            var  result = 0;

            do
            {
                try
                {
                    result = await base.SaveChangesAsync().ConfigureAwait(false);

                    fail = false;
                    return(await Task.Factory.StartNew(() => ExecuteResult <int> .CreateSuccess(result, "已提交")));
                }
                catch (DbEntityValidationException ex)
                {
                    ex.EntityValidationErrors.Each(_e =>
                    {
                        var errs = _e.Entry.GetValidationResult().ValidationErrors.ValidationErrorAppendToString();
                        YmatouLoggingService.Error("实体:{0},验证错误:{1}", _e.Entry.Entity.GetType().Name, errs);
                    });
                    return(ExecuteResult <int> .CreateFaile(0, "提交失败"));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    fail = true;
                    ex.Entries.Each(entry =>
                    {
                        if (entry != null)
                        {
                            if (entry.State == EntityState.Added || entry.State == EntityState.Modified)
                            {
                                //Context中实体的值覆盖db中实体的值
                                var currentVal = entry.CurrentValues;       //当前值
                                var dbVal      = entry.GetDatabaseValues(); //获取数据库中的值
                                entry.OriginalValues.SetValues(dbVal);
                                entry.CurrentValues.SetValues(dbVal.Clone());
                                entry.State = EntityState.Modified;
                            }
                            else
                            {
                                //数据库中的覆盖entity中的值
                                entry.Reload();
                            }
                        }
                    });
                    var _ex = ex.GetBaseException() as SqlException;
                    if (_ex != null && (_ex.Number == 2627 || _ex.Number == 515))
                    {
                        YmatouLoggingService.Debug("EFUnitOfWork DbUpdateConcurrencyException  repeat insert,{0}", _ex.Message);
                    }
                    else
                    {
                        YmatouLoggingService.Error("EFUnitOfWork DbUpdateConcurrencyException entity : {0},err:{1},重试 {2} 次", ex.Entries.TrySerializeEntity(),
                                                   ex.ToString(), tmpRetry);
                    }
                }
//                catch (DbUpdateException ex)
//                {
//                    fail = true;
//                    var _ex = ex.GetBaseException() as SqlException;
//                    if (_ex != null && (_ex.Number == 2627 || _ex.Number == 515))
//                        YmatouLoggingService.Debug("EFUnitOfWork DbUpdateException  repeat insert,{0}", _ex.Message);
//                    else
//                        YmatouLoggingService.Error("EFUnitOfWork DbUpdateException entity : {0},err:{1},重试 {2} 次",
//                            ex.Entries.TrySerializeEntity(),
//                            ex.ToString(), tmpRetry);
//                }
            } while (fail && tmpRetry-- > 0);
            return(await Task.Factory.StartNew(() => new ExecuteResult <int>(fail == false, fail == false ? "执行成功" : "提交失败", result)));
        }