From c422b8182941ff19668c64fb829e22fde0eef9ae Mon Sep 17 00:00:00 2001 From: easy <45551150+easy999000@users.noreply.github.com> Date: Wed, 13 Dec 2023 22:33:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E9=80=9A=E8=BF=87DTO=20?= =?UTF-8?q?=E8=BF=9B=E8=A1=8Cupdate=E7=9A=84=E6=96=B9=E6=B3=95,=20?= =?UTF-8?q?=E7=89=B9=E7=82=B9=E6=98=AF,=E5=BF=BD=E7=95=A5=E5=80=BC?= =?UTF-8?q?=E4=B8=BAnull=E7=9A=84=E5=B1=9E=E6=80=A7.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 说明:在开发的时候,希望代码能实现更高的复用,那么一个更新的方法,我希望在多个不同的场景都可以使用. 但是不同场景涉及的字段有事不一样的,每次都把完整的DTO实体生成出来,既繁琐,又容易出错, 于是想升级这个方法, 不想update的字段,为空就好了,这样不会影响数据库中的原有值. --- .../Internal/CommonProvider/UpdateProvider.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/FreeSql/Internal/CommonProvider/UpdateProvider.cs b/FreeSql/Internal/CommonProvider/UpdateProvider.cs index 4d8ab3c9..2b1551db 100644 --- a/FreeSql/Internal/CommonProvider/UpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/UpdateProvider.cs @@ -777,6 +777,38 @@ namespace FreeSql.Internal.CommonProvider } return this; } + public IUpdate SetDtoIgnoreNull(object dto) + { + if (dto == null) return this; + if (dto is Dictionary) + { + var dic = dto as Dictionary; + foreach (var kv in dic) + { + if (kv.Value == null) + { + continue; + } + if (_table.ColumnsByCs.TryGetValue(kv.Key, out var trycol) == false) continue; + if (_ignore.ContainsKey(trycol.Attribute.Name)) continue; + SetPriv(trycol, kv.Value); + } + return this; + } + var dtoProps = dto.GetType().GetProperties(); + foreach (var dtoProp in dtoProps) + { + var v3 = dtoProp.GetValue(dto, null); + if (v3 == null) + { + continue; + } + if (_table.ColumnsByCs.TryGetValue(dtoProp.Name, out var trycol) == false) continue; + if (_ignore.ContainsKey(trycol.Attribute.Name)) continue; + SetPriv(trycol, v3); + } + return this; + } public IUpdate Where(Expression> exp) => WhereIf(true, exp); public IUpdate WhereIf(bool condition, Expression> exp)