diff --git a/FreeSql/Extensions/FreeSqlGlobalExtensions.cs b/FreeSql/Extensions/FreeSqlGlobalExtensions.cs index ff7286c1..113cb257 100644 --- a/FreeSql/Extensions/FreeSqlGlobalExtensions.cs +++ b/FreeSql/Extensions/FreeSqlGlobalExtensions.cs @@ -876,6 +876,7 @@ SELECT "); /// public static InsertDictImpl InsertDict(this IFreeSql freesql, Dictionary source) { + LocalReplaceDictDBNullValue(source); var insertDict = new InsertDictImpl(freesql); insertDict._insertProvider.AppendData(source); return insertDict; @@ -887,10 +888,20 @@ SELECT "); /// public static InsertDictImpl InsertDict(this IFreeSql freesql, IEnumerable> source) { + if (source?.Any() == true) foreach (var dict in source) LocalReplaceDictDBNullValue(dict); var insertDict = new InsertDictImpl(freesql); insertDict._insertProvider.AppendData(source); return insertDict; } + static void LocalReplaceDictDBNullValue(Dictionary dict) + { + if (dict == null) return; + foreach (var key in dict.Keys) + { + var val = dict[key]; + if (val == DBNull.Value) dict[key] = null; + } + } /// /// 更新数据字典 Dictionary<string, object> /// @@ -898,6 +909,7 @@ SELECT "); /// public static UpdateDictImpl UpdateDict(this IFreeSql freesql, Dictionary source) { + LocalReplaceDictDBNullValue(source); var updateDict = new UpdateDictImpl(freesql); updateDict._updateProvider.SetSource(source); return updateDict; @@ -909,6 +921,7 @@ SELECT "); /// public static UpdateDictImpl UpdateDict(this IFreeSql freesql, IEnumerable> source) { + if (source?.Any() == true) foreach (var dict in source) LocalReplaceDictDBNullValue(dict); var updateDict = new UpdateDictImpl(freesql); updateDict._updateProvider.SetSource(source); return updateDict; @@ -929,12 +942,14 @@ SELECT "); /// public static InsertOrUpdateDictImpl InsertOrUpdateDict(this IFreeSql freesql, Dictionary source) { + LocalReplaceDictDBNullValue(source); var insertOrUpdateDict = new InsertOrUpdateDictImpl(freesql); insertOrUpdateDict._insertOrUpdateProvider.SetSource(source); return insertOrUpdateDict; } public static InsertOrUpdateDictImpl InsertOrUpdateDict(this IFreeSql freesql, IEnumerable> source) { + if (source?.Any() == true) foreach (var dict in source) LocalReplaceDictDBNullValue(dict); var insertOrUpdateDict = new InsertOrUpdateDictImpl(freesql); insertOrUpdateDict._insertOrUpdateProvider.SetSource(source); return insertOrUpdateDict; @@ -946,6 +961,7 @@ SELECT "); /// public static DeleteDictImpl DeleteDict(this IFreeSql freesql, Dictionary source) { + LocalReplaceDictDBNullValue(source); var deleteDict = new DeleteDictImpl(freesql); UpdateProvider>.GetDictionaryTableInfo(source, deleteDict._deleteProvider._orm, ref deleteDict._deleteProvider._table); var primarys = UpdateDictImpl.GetPrimarys(deleteDict._deleteProvider._table, source.Keys.ToArray()); @@ -959,6 +975,7 @@ SELECT "); /// public static DeleteDictImpl DeleteDict(this IFreeSql freesql, IEnumerable> source) { + if (source?.Any() == true) foreach (var dict in source) LocalReplaceDictDBNullValue(dict); DeleteDictImpl deleteDict = null; if (source.Select(a => string.Join(",", a.Keys)).Distinct().Count() == 1) {