diff --git a/FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs b/FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs index 2f309f0b..343e8e3c 100644 --- a/FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs @@ -94,10 +94,7 @@ 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) @@ -116,32 +113,11 @@ 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; - if (source is IEnumerable> dicType) - { - var tempDict = new Dictionary(); - foreach (var item in dicType) - { - foreach (string key in item.Keys) - { - if (!tempDict.ContainsKey(key) && !(item[key] is null)) - { - tempDict[key] = item[key]; - } - } - } - UpdateProvider.GetDictionaryTableInfo(tempDict, _orm, ref _table); - } - else - { - UpdateProvider.GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table); - } + UpdateProvider.GetDictionaryTableInfo(source, _orm, ref _table); AuditDataValue(this, source, _orm, _table, _auditValueChangedDict); _source.AddRange(source.Where(a => a != null)); @@ -248,7 +224,7 @@ namespace FreeSql.Internal.CommonProvider } } - byte _SplitSourceByIdentityValueIsNullFlag = 0;//防止重复计算 SplitSource + byte _SplitSourceByIdentityValueIsNullFlag = 0 ;//防止重复计算 SplitSource /// /// 如果实体类有自增属性,分成两个 List,有值的Item1 merge,无值的Item2 insert /// @@ -257,23 +233,15 @@ 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); } @@ -291,10 +259,7 @@ namespace FreeSql.Internal.CommonProvider }).GroupBy(a => a.splitKey, a => a.item).Select(a => a.ToList()).ToArray(); return atarr; } - return new[] - { - loc1 - }; + return new[] { loc1 }; } } diff --git a/FreeSql/Internal/CommonProvider/InsertProvider.cs b/FreeSql/Internal/CommonProvider/InsertProvider.cs index f572b8b7..bc6c7e2d 100644 --- a/FreeSql/Internal/CommonProvider/InsertProvider.cs +++ b/FreeSql/Internal/CommonProvider/InsertProvider.cs @@ -133,7 +133,7 @@ namespace FreeSql.Internal.CommonProvider { if (source != null) { - UpdateProvider.GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table); + UpdateProvider.GetDictionaryTableInfo(source, _orm, ref _table); AuditDataValue(this, source, _orm, _table, _auditValueChangedDict); _source.AddRange(source); } @@ -144,7 +144,7 @@ namespace FreeSql.Internal.CommonProvider if (source != null) { source = source.Where(a => a != null).ToList(); - UpdateProvider.GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table); + UpdateProvider.GetDictionaryTableInfo(source, _orm, ref _table); AuditDataValue(this, source, _orm, _table, _auditValueChangedDict); _source.AddRange(source); diff --git a/FreeSql/Internal/CommonProvider/UpdateProvider.cs b/FreeSql/Internal/CommonProvider/UpdateProvider.cs index 0d825c45..5e6a3bef 100644 --- a/FreeSql/Internal/CommonProvider/UpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/UpdateProvider.cs @@ -451,6 +451,20 @@ namespace FreeSql.Internal.CommonProvider } } + public static void GetDictionaryTableInfo(IEnumerable source, IFreeSql orm, ref TableInfo table) + { + if (table == null && typeof(T1) == typeof(Dictionary) && source is IEnumerable> dicType) + { + var tempDict = new Dictionary(); + foreach (var item in dicType) + foreach (string key in item.Keys) + if (!tempDict.ContainsKey(key) && !(item[key] is null)) + tempDict[key] = item[key]; + UpdateProvider>.GetDictionaryTableInfo(tempDict, orm, ref table); + return; + } + GetDictionaryTableInfo(source.FirstOrDefault(), orm, ref table); + } public static void GetDictionaryTableInfo(T1 source, IFreeSql orm, ref TableInfo table) { if (table == null && typeof(T1) == typeof(Dictionary)) @@ -493,7 +507,7 @@ namespace FreeSql.Internal.CommonProvider public IUpdate SetSource(IEnumerable source, Expression> tempPrimarys = null, bool ignoreVersion = false) { if (source == null || source.Any() == false) return this; - GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table); + GetDictionaryTableInfo(source, _orm, ref _table); AuditDataValue(this, source, _orm, _table, _auditValueChangedDict); _source.AddRange(source.Where(a => a != null));