/// <summary>
        ///     修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dbs"></param>
        /// <param name="t">如果sql为null,则根据t的主键进行修改</param>
        /// <param name="updateProperties">要修改的属性集合</param>
        /// <param name="sql">按条件修改</param>
        /// <returns></returns>
        public static async Task <int> Update <T>(this IUnitOfWork dbs, T t, IList <string> updateProperties, IDbTransaction transaction = null, SqlQuery sql = null)
            where T : class
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            var db = dbs.DbConnection;

            if (sql == null)
            {
                sql = SqlQuery <T> .Builder(dbs);
            }
            sql = sql.AppendParam(t)
                  .SetExcProperties <T>(updateProperties);

            #region 之前的数据
            Type Ts     = t.GetType();
            var  prikey = Ts.GetProperty(DapperCacheCommon.GetPrimary(sql._ModelDes).Field).GetValue(t, null).ToString();
            var  obj    = dbs.DbConnection.Query(sql.QuerySqlById.Replace("@id", prikey.TrimEnd(',').SqlRemoveStr())).FirstOrDefault();
            #endregion

            //SqlLog log = new SqlLog
            //{
            //    CreateTime = DateTime.Now,
            //    OperateSql = sql.UpdateSql,
            //    Parameter = sql.Param == null ? "" : sql.Param.ToString()
            //};

            //DataLog datalog = new DataLog
            //{
            //    OperateType = OperateType.Edit.ToString(),
            //    OperateTable = sql._ModelDes.TableName,
            //    OperationBefore = JsonConvert.SerializeObject(obj),
            //    OperationAfterData = JsonConvert.SerializeObject(t),
            //};

            var result = await db.ExecuteAsync(sql.UpdateSql, sql.Param, transaction);

            stopwatch.Stop();

            //log.EndDateTime = DateTime.Now;
            //log.ElapsedTime = stopwatch.Elapsed.TotalSeconds;
            //WriteSqlLog(log);
            //WriteDataLog(datalog);
            return(result);
        }
Пример #2
0
        /// <summary>
        /// 奖其它参数添加到参数对象中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        internal SqlQuery AppendParam <T>(T t) where T : class
        {
            if (_Param == null)
            {
                _Param = new List <DynamicPropertyModel>();
            }
            var model = DapperCacheCommon.GetModelDes <T>();

            foreach (var item in model.Properties)
            {
                var value = model.ClassType.GetProperty(item.Field).GetValue(t, null);
                ParamValues.Add(item.Field, value);
                var pmodel = new DynamicPropertyModel
                {
                    Name         = item.Field,
                    PropertyType = value != null?value.GetType() : typeof(String)
                };
                _Param.Add(pmodel);
            }
            return(this);
        }