diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index dc0203b8..d9f91124 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -110,6 +110,13 @@ 清空状态数据 + + + 根据 lambda 条件删除数据 + + + + 添加 diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 21df8587..5c768294 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -2000,6 +2000,17 @@ 参数 + + + 设置更新的列 + + SetDto(new { title = "xxx", clicks = 2 }) + + SetDto(new Dictionary<string, object> { ["title"] = "xxx", ["clicks"] = 2 }) + + dto 或 Dictionary<string, object> + + lambda表达式条件,仅支持实体基础成员(不包含导航对象) diff --git a/FreeSql/Interface/Curd/IUpdate.cs b/FreeSql/Interface/Curd/IUpdate.cs index 8595b648..5e79eb95 100644 --- a/FreeSql/Interface/Curd/IUpdate.cs +++ b/FreeSql/Interface/Curd/IUpdate.cs @@ -56,6 +56,7 @@ namespace FreeSql /// 实体集合 /// IUpdate SetSource(IEnumerable source); + /// /// 忽略的列,IgnoreColumns(a => a.Name) | IgnoreColumns(a => new{a.Name,a.Time}) | IgnoreColumns(a => new[]{"name","time"}) /// @@ -107,6 +108,17 @@ namespace FreeSql /// IUpdate SetRaw(string sql, object parms = null); + /// + /// 设置更新的列 + /// + /// SetDto(new { title = "xxx", clicks = 2 }) + /// + /// SetDto(new Dictionary<string, object> { ["title"] = "xxx", ["clicks"] = 2 }) + /// + /// dto 或 Dictionary<string, object> + /// + IUpdate SetDto(object dto); + /// /// lambda表达式条件,仅支持实体基础成员(不包含导航对象) /// 若想使用导航对象,请使用 ISelect.ToUpdate() 方法 diff --git a/FreeSql/Internal/CommonProvider/UpdateProvider.cs b/FreeSql/Internal/CommonProvider/UpdateProvider.cs index 141abdd9..0b358e40 100644 --- a/FreeSql/Internal/CommonProvider/UpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/UpdateProvider.cs @@ -334,26 +334,31 @@ namespace FreeSql.Internal.CommonProvider return this; } + protected void SetPriv(ColumnInfo col, object value) + { + object paramVal = null; + if (value != null) + { + if (col.Attribute.MapType == value.GetType()) paramVal = value; + else paramVal = Utils.GetDataReaderValue(col.Attribute.MapType, value); + } + _set.Append(", ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = "); + if (_noneParameter) + { + _set.Append(_commonUtils.GetNoneParamaterSqlValue(_params, col.Attribute.MapType, paramVal)); + } + else + { + _set.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, $"{_commonUtils.QuoteParamterName("p_")}{_params.Count}")); + _commonUtils.AppendParamter(_params, null, col, col.Attribute.MapType, paramVal); + } + } public IUpdate Set(Expression> column, TMember value) { var cols = new List(); _commonExpression.ExpressionSelectColumn_MemberAccess(null, cols, SelectTableInfoType.From, column?.Body, true, null); if (cols.Count != 1) return this; - var col = cols.First(); - object paramVal = null; - if (col.Column.Attribute.MapType == typeof(TMember)) paramVal = value; - else paramVal = Utils.GetDataReaderValue(col.Column.Attribute.MapType, value); - _set.Append(", ").Append(_commonUtils.QuoteSqlName(col.Column.Attribute.Name)).Append(" = "); - if (_noneParameter) - { - _set.Append(_commonUtils.GetNoneParamaterSqlValue(_params, col.Column.Attribute.MapType, paramVal)); - } - else - { - _set.Append(_commonUtils.QuoteWriteParamter(col.Column.Attribute.MapType, $"{_commonUtils.QuoteParamterName("p_")}{_params.Count}")); - _commonUtils.AppendParamter(_params, null, col.Column, col.Column.Attribute.MapType, paramVal); - } - //foreach (var t in _source) Utils.FillPropertyValue(t, tryf.CsName, value); + SetPriv(cols.First().Column, value); return this; } public IUpdate Set(Expression> exp) @@ -424,6 +429,27 @@ namespace FreeSql.Internal.CommonProvider return this; } + public IUpdate SetDto(object dto) + { + if (dto == null) return this; + if (dto is Dictionary) + { + var dic = dto as Dictionary; + foreach (var kv in dic) + { + if (_table.ColumnsByCs.TryGetValue(kv.Key, out var trycol) == false) continue; + SetPriv(trycol, kv.Value); + } + } + var dtoProps = dto.GetType().GetProperties(); + foreach (var dtoProp in dtoProps) + { + if (_table.ColumnsByCs.TryGetValue(dtoProp.Name, out var trycol) == false) continue; + SetPriv(trycol, dtoProp.GetValue(dto, null)); + } + return this; + } + public IUpdate Where(Expression> expression) => this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, expression?.Body, null, _params)); public IUpdate Where(string sql, object parms = null) {