/// <summary>
        /// 将模式对象的修改提交到数据库
        /// </summary>
        /// <param name="obj">对其进行更新的<typeparamref name="T"/>对象。</param>
        public void Update(T obj)
        {
            obj.NullCheck("obj");

            this.MergeExistsObjectInfo(obj);

            EntityInstanceUpdateActionCollection actions = GetActions("Update");

            actions.Prepare(obj);

            using (TransactionScope scope = TransactionScopeFactory.Create())
            {
                DEEntityInstanceBase existData = GetExistedObject(obj);
                string sql = string.Empty;
                if (existData != null)
                {
                    sql = EntityInstanceUpdateSqlBuilder.Instance.ToUpdateSql(obj, this.GetMappingInfo());
                    DbHelper.RunSqlReturnScalar(sql, this.GetConnectionName());
                }
                else
                {
                    sql = EntityInstanceUpdateSqlBuilder.Instance.ToInsertSql(obj, this.GetMappingInfo());
                    DbHelper.RunSqlReturnScalar(sql, this.GetConnectionName());
                }

                //执行update  如果update失败执行insert
                //string sql = EntityInstanceUpdateSqlBuilder.Instance.ToUpdateSql(obj, this.GetMappingInfo());
                //try
                //{
                //    DbHelper.RunSqlReturnScalar(sql, this.GetConnectionName());
                //    DbHelper.RunSqlReturnScalar(sql, this.GetConnectionName());
                //}
                //catch (Exception)
                //{
                //    sql = EntityInstanceUpdateSqlBuilder.Instance.ToInsertSql(obj, this.GetMappingInfo());
                //    DbHelper.RunSqlReturnScalar(sql, this.GetConnectionName());
                //}

                //SCInstanceActionContext.Current.TimePoint.IsMinValue(() => SCInstanceActionContext.Current.TimePoint = dt);
                //string sql = EntityInstanceUpdateSqlBuilder.Instance.ToInsertSql(obj, this.GetMappingInfo());

                actions.Persist(obj);

                scope.Complete();
            }
        }
示例#2
0
        /// <summary>
        /// 获取配置的操作
        /// </summary>
        /// <param name="operation"></param>
        /// <returns></returns>
        public EntityInstanceUpdateActionCollection GetActions(string operation)
        {
            EntityInstanceUpdateActionCollection actions = new EntityInstanceUpdateActionCollection();

            foreach (EntityInstanceUpdateActionConfigurationElement actionElem in this.Actions)
            {
                try
                {
                    if (actionElem.Operation == operation)
                    {
                        actions.Add((IEntityInstanceUpdateAction)actionElem.CreateInstance());
                    }
                }
                catch (Exception ex)
                {
                    WriteToLog(ex);
                }
            }

            return(actions);
        }
        /// <summary>
        /// 更新模式对象的状态到数据库
        /// </summary>
        /// <param name="obj">对其进行更新的<typeparamref name="T"/>对象。</param>
        /// <param name="status">表示状态的<see cref="SchemaObjectStatus"/>值之一。</param>
        public void UpdateStatus(T obj, SchemaObjectStatus status)
        {
            obj.Status = status;

            string sql = EntityInstanceUpdateStatusSqlBuilder.Instance.ToUpdateSql(obj, this.GetMappingInfo());

            EntityInstanceUpdateActionCollection actions = GetActions("UpdateStatus");

            actions.Prepare(obj);

            using (TransactionScope scope = TransactionScopeFactory.Create())
            {
                DateTime dt = (DateTime)DbHelper.RunSqlReturnScalar(sql, this.GetConnectionName());

                SCInstanceActionContext.Current.TimePoint.IsMinValue(() => SCInstanceActionContext.Current.TimePoint = dt);

                actions.Persist(obj);

                scope.Complete();
            }
        }