示例#1
0
文件: DbTable`T.cs 项目: tofx/data
        private void ExecuteModelDbOperation(IDbQueryStrategy strategy, TModel model = null)
        {
            Exception exception = null;
            var       dbparams  = (model == null)
                ? new List <IDbDataParameter>()
                : DbModelBindingHelper.BindingModelToParameters(model, ModelStrategy.PropertyStrategies, strategy.GetDbParameters());

            _serviceProvider.Open();

            try
            {
                if (_serviceProvider.Execute(strategy.GetDbQueryScript(), dbparams) == 0)
                {
                    throw new DbChangeNoAffectException();
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            _serviceProvider.Close();

            if (exception != null)
            {
                throw new DbAggregateException(exception);
            }
        }
示例#2
0
文件: DbTable`T.cs 项目: tofx/data
        private void ExecuteModelDbOperation(IDbQueryStrategy strategy, IEnumerable <TModel> models)
        {
            var exceptions = new Dictionary <int, Exception>();

            _serviceProvider.Open();
            _serviceProvider.BeginTransaction();

            var modelIdx = 0;

            foreach (var model in models)
            {
                try
                {
                    var dbparams = DbModelBindingHelper.BindingModelToParameters(
                        model, ModelStrategy.PropertyStrategies, strategy.GetDbParameters());
                    if (_serviceProvider.Execute(strategy.GetDbQueryScript(), dbparams) == 0)
                    {
                        throw new DbChangeNoAffectException();
                    }
                }
                catch (Exception ex)
                {
                    exceptions.Add(modelIdx, ex);
                }

                modelIdx++;
            }

            if (!exceptions.Any())
            {
                _serviceProvider.Commit();
                _serviceProvider.Close();
            }
            else
            {
                _serviceProvider.Rollback();
                _serviceProvider.Close();
                throw new DbAggregateException(exceptions);
            }
        }