From d9093c840aadb2aca551e83e85b8542cef06c56e Mon Sep 17 00:00:00 2001 From: Tony Han Date: Wed, 6 Jul 2022 19:37:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=AD=97=E5=85=B8?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=89=B9=E9=87=8F=E6=8F=92=E5=85=A5=E6=97=B6?= =?UTF-8?q?=20=E4=B8=A2=E5=A4=B1=E5=AD=97=E6=AE=B5=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CommonProvider/InsertOrUpdateProvider.cs | 54 +++++++++++++++---- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs b/FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs index fb6efafa..5d97f059 100644 --- a/FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs @@ -1,6 +1,7 @@ using FreeSql.Extensions.EntityUtil; using FreeSql.Internal.Model; using System; +using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.Common; @@ -93,7 +94,10 @@ namespace FreeSql.Internal.CommonProvider public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table, Dictionary changedDict) { if (data == null || table == null) return; - if (typeof(T1) == typeof(object) && new[] { table.Type, table.TypeLazy }.Contains(data.GetType()) == false) + if (typeof(T1) == typeof(object) && new[] + { + table.Type, table.TypeLazy + }.Contains(data.GetType()) == false) throw new Exception(CoreStrings.DataType_AsType_Inconsistent(data.GetType().DisplayCsharp(), table.Type.DisplayCsharp())); if (orm.Aop.AuditValueHandler == null) return; foreach (var col in table.Columns.Values) @@ -112,11 +116,32 @@ namespace FreeSql.Internal.CommonProvider } } - public IInsertOrUpdate SetSource(T1 source) => this.SetSource(new[] { source }); + public IInsertOrUpdate SetSource(T1 source) => this.SetSource(new[] + { + source + }); public IInsertOrUpdate SetSource(IEnumerable source, Expression> tempPrimarys = null) { if (source == null || source.Any() == false) return this; - UpdateProvider.GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table); + if (source is IEnumerable> dicType) + { + var tempDict = new Dictionary(); + foreach (var item in dicType) + { + foreach (string key in item.Keys) + { + if (!tempDict.ContainsKey(key)) + { + tempDict[key] = item[key]; + } + } + } + UpdateProvider.GetDictionaryTableInfo(tempDict, _orm, ref _table); + } + else + { + UpdateProvider.GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table); + } AuditDataValue(this, source, _orm, _table, _auditValueChangedDict); _source.AddRange(source.Where(a => a != null)); @@ -223,7 +248,7 @@ namespace FreeSql.Internal.CommonProvider } } - byte _SplitSourceByIdentityValueIsNullFlag = 0; //防止重复计算 SplitSource + byte _SplitSourceByIdentityValueIsNullFlag = 0;//防止重复计算 SplitSource /// /// 如果实体类有自增属性,分成两个 List,有值的Item1 merge,无值的Item2 insert /// @@ -232,15 +257,23 @@ namespace FreeSql.Internal.CommonProvider public NativeTuple[], List[]> SplitSourceByIdentityValueIsNull(List source) { if (source.Any() == false) return NativeTuple.Create(new List[0], new List[0]); - if (_SplitSourceByIdentityValueIsNullFlag == 1) return NativeTuple.Create(new[] { source }, new List[0]); - if (_SplitSourceByIdentityValueIsNullFlag == 2) return NativeTuple.Create(new List[0], new[] { source }); + if (_SplitSourceByIdentityValueIsNullFlag == 1) + return NativeTuple.Create(new[] + { + source + }, new List[0]); + if (_SplitSourceByIdentityValueIsNullFlag == 2) + return NativeTuple.Create(new List[0], new[] + { + source + }); if (IdentityColumn == null || _tempPrimarys != _table.Primarys) return NativeTuple.Create(LocalSplitSourceByAsTable(source), new List[0]); var item1 = new List(); var item2 = new List(); foreach (var item in source) { if (object.Equals(_orm.GetEntityValueWithPropertyName(_table.Type, item, IdentityColumn.CsName), IdentityColumn.CsType.CreateInstanceGetDefaultValue())) - item2.Add(item); //自增无值的,记录为直接插入 + item2.Add(item);//自增无值的,记录为直接插入 else item1.Add(item); } @@ -258,7 +291,10 @@ namespace FreeSql.Internal.CommonProvider }).GroupBy(a => a.splitKey, a => a.item).Select(a => a.ToList()).ToArray(); return atarr; } - return new[] { loc1 }; + return new[] + { + loc1 + }; } } @@ -451,4 +487,4 @@ namespace FreeSql.Internal.CommonProvider } #endif } -} +} \ No newline at end of file