From 76c7a2f757a6e770db2b857b042ed284ab7e6fcd Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 12 Mar 2023 12:25:50 +0800 Subject: [PATCH] Update i gnoring NULL columns supports batch --- .../UpdateProvider/UpdateableHelper.cs | 32 ++++++++++++------- .../UpdateProvider/UpdateableProvider.cs | 5 +-- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs index 29ee800a5..9abd8147f 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs @@ -11,6 +11,11 @@ namespace SqlSugar { public partial class UpdateableProvider : IUpdateable where T : class, new() { + private bool IsUpdateNullByList() + { + return this.UpdateObjs.Count() > 1 && (this.UpdateBuilder.IsNoUpdateNull || this.UpdateBuilder.IsNoUpdateDefaultValue); + } + private int DatasTrackingExecommand() { var trakRows = 0; @@ -97,22 +102,25 @@ namespace SqlSugar } private void AppendTracking(T item, IUpdateable newUpdateable) { - var trackingData = this.Context.TempItems.FirstOrDefault(it => it.Key.StartsWith("Tracking_" + item.GetHashCode())); - var diffColumns = FastCopy.GetDiff(item, (T)trackingData.Value); - if (diffColumns.Count > 0) + if (IsTrakingData() || IsTrakingDatas()) { - var pks = EntityInfo.Columns - .Where(it => it.IsPrimarykey).Select(it => it.PropertyName).ToList(); - diffColumns = diffColumns.Where(it => !pks.Contains(it)).ToList(); + var trackingData = this.Context.TempItems.FirstOrDefault(it => it.Key.StartsWith("Tracking_" + item.GetHashCode())); + var diffColumns = FastCopy.GetDiff(item, (T)trackingData.Value); if (diffColumns.Count > 0) { - newUpdateable.UpdateColumns(diffColumns.ToArray()); + var pks = EntityInfo.Columns + .Where(it => it.IsPrimarykey).Select(it => it.PropertyName).ToList(); + diffColumns = diffColumns.Where(it => !pks.Contains(it)).ToList(); + if (diffColumns.Count > 0) + { + newUpdateable.UpdateColumns(diffColumns.ToArray()); + } + } + else + { + (newUpdateable as UpdateableProvider).UpdateObjs = new T[] { null }; + newUpdateable.UpdateBuilder.DbColumnInfoList = new List(); } - } - else - { - (newUpdateable as UpdateableProvider).UpdateObjs = new T[] { null }; - newUpdateable.UpdateBuilder.DbColumnInfoList = new List(); } } private void AppendSets() diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index 0560d8259..3bb6bbb7c 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -80,7 +80,7 @@ namespace SqlSugar public virtual int ExecuteCommand() { - if (this.IsTrakingDatas()) + if (this.IsTrakingDatas() || IsUpdateNullByList()) { int trakRows = DatasTrackingExecommand(); return trakRows; @@ -95,6 +95,7 @@ namespace SqlSugar return result; } + public bool ExecuteCommandHasChange() { return this.ExecuteCommand() > 0; @@ -277,7 +278,7 @@ namespace SqlSugar public IUpdateable IgnoreColumns(bool ignoreAllNullColumns, bool isOffIdentity = false, bool ignoreAllDefaultValue = false) { - Check.Exception(this.UpdateObjs.Count() > 1 && ignoreAllNullColumns, ErrorMessage.GetThrowMessage("ignoreNullColumn NoSupport batch insert", "ignoreNullColumn 不支持批量操作")); + //Check.Exception(this.UpdateObjs.Count() > 1 && ignoreAllNullColumns, ErrorMessage.GetThrowMessage("ignoreNullColumn NoSupport batch insert", "ignoreNullColumn 不支持批量操作")); UpdateBuilder.IsOffIdentity = isOffIdentity; if (this.UpdateBuilder.LambdaExpressions == null) this.UpdateBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig);