/// <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()); } }
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); } }
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 Where(string sql, Parameter param) { _builder.Where(sql, param); return(this); }