private static void autosetUpdate <TEntity>(TEntity entity, IUpdateBuilder builder) where TEntity : class
        {
            //有并发控制
            if (entity != null && entity is IHasConcurrencyStamp concurrencyStampEntity && !string.IsNullOrEmpty(concurrencyStampEntity.ConcurrencyStamp))
            {
                builder.Column("ConcurrencyStamp", Guid.NewGuid().ToString());
                builder.Where("ConcurrencyStamp=@OldConcurrencyStamp", new SqlBuilderParameter("OldConcurrencyStamp", concurrencyStampEntity.ConcurrencyStamp));
            }

            var type = typeof(TEntity);

            if (typeof(IHasModificationTime).IsAssignableFrom(type))
            {
                builder.Column("LastModificationTime", (entity as IHasModificationTime)?.LastModificationTime ?? DateTime.Now);
            }
            else
            {
                return;
            }

            if (entity != null && type.GetInterfaces().Any(m => m.IsGenericType && m.GetGenericTypeDefinition() == typeof(IModificationAudited <>)))
            {
                builder.Column("LastModifierId", entity != null ? type.GetProperty("LastModifierId").GetValue(entity, null) : 0);
                builder.Column("LastModifierName", entity != null ? (type.GetProperty("LastModifierName").GetValue(entity, null) ?? string.Empty) : string.Empty);
            }
        }
        /// <summary>
        /// 更新数据表
        /// </summary>
        /// <param name="executeModel"></param>
        /// <returns></returns>
        public int Update(ExecuteModel executeModel)
        {
            using (var db = new DbContext())
            {
                //生成操作时间
                //executeModel.Entity.oper_time = db.Database().GetDateTime;

                //设置不立即设置为根据主键更新,后续根据和条件更新
                IUpdateBuilder updateBuilder = db.Update(executeModel.Entity, false);
                if (executeModel.Columns != null && executeModel.Columns.Length > 0)
                {//如果更新有指定列,则指定列
                    updateBuilder.Column(executeModel.Columns);
                }

                //获取条件sql
                var whereSql = GetConitionsql(executeModel.Conditions);
                updateBuilder.Where(whereSql);
                executeModel.Conditions?.ForEach(p =>
                {//先设置参数,组装SQL
                    updateBuilder.Parameters(p.Key, p.Value);
                });

                //执行更新
                return(updateBuilder.Execute());
            }
        }
Exemple #3
0
        public static IUpdateBuilder Column(this IUpdateBuilder builder, bool condition, Func <string> func)
        {
            builder = builder ?? throw new ArgumentNullException(nameof(builder));
            func    = func ?? throw new ArgumentNullException(nameof(func));

            if (condition)
            {
                return(builder.Column(func.Invoke()));
            }

            return(builder);
        }
Exemple #4
0
        public static IUpdateBuilder Column(this IUpdateBuilder builder, string columnName, object value, DbType?dbType = null, ParameterDirection?direction = null, int?size = null)
        {
            builder = builder ?? throw new ArgumentNullException(nameof(builder));

            if (string.IsNullOrEmpty(columnName))
            {
                throw new ArgumentException($"{nameof(columnName)}不能为空", nameof(columnName));
            }
            //if (value == null)
            //    throw new ArgumentException($"{nameof(value)}不能为空", nameof(value));

            builder.Column(new SqlBuilderParameter(columnName, value, dbType, direction, size));

            return(builder);
        }
Exemple #5
0
 public bool Update(T t)
 {
     using (var context = _Context())
     {
         IUpdateBuilder <T> temp = context.Update <T>(tableName, t);
         foreach (var item in typeof(T).GetProperties())
         {
             if (item.CustomAttributes.FirstOrDefault(c => c.AttributeType.Name == "PrimaryKey") != null)
             {
                 temp.Where(item.Name, item.GetValue(t));
             }
             else
             {
                 temp = temp.Column(item.Name, item.GetValue(t));
             }
         }
         return(temp.Execute() > 0);
     }
 }
 public IUpdateBuilder Column(string sql)
 {
     _builder.Column(sql);
     return(this);
 }