From ede318c60e33a699fd93a15f48341a46d550d7c0 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Wed, 25 Jun 2025 23:06:50 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=20WithMemory=20=E9=A6=96?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA=20NULL=20=E5=80=BC=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5=EF=BC=9B#2047?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.DbContext/FreeSql.DbContext.xml | 8 ++++++ .../CommonProvider/InsertOrUpdateProvider.cs | 27 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 9721afddf..7cc8aea60 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -764,5 +764,13 @@ + + + 批量注入 Repository,可以参考代码自行调整 + + + + + diff --git a/FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs b/FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs index 736eba535..5c2077336 100644 --- a/FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs @@ -295,7 +295,32 @@ namespace FreeSql.Internal.CommonProvider else { object val = col.GetDbValue(d); - sb.Append(_commonUtils.RewriteColumn(col, _commonUtils.GetNoneParamaterSqlValue(dbParams, "cu", col, col.Attribute.MapType, val))); + var valsql = _commonUtils.RewriteColumn(col, _commonUtils.GetNoneParamaterSqlValue(dbParams, "cu", col, col.Attribute.MapType, val)); + if (didx == 0 && valsql == "NULL") + { + var dbtype = _orm.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype; + if (!string.IsNullOrWhiteSpace(dbtype)) + { + switch (_orm.Ado.DataType) + { + case DataType.MsAccess: + case DataType.Odbc: + case DataType.Custom: + break; // MsAccess 不支持 cast(null as xxx),直接用 NULL + case DataType.PostgreSQL: + case DataType.OdbcPostgreSQL: + case DataType.CustomPostgreSQL: + case DataType.KingbaseES: + case DataType.ShenTong: + valsql = $"NULL::{_orm.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype}"; + break; + default: + valsql = $"cast(NULL as {_orm.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype})"; + break; + } + } + } + sb.Append(valsql); } if (didx == 0) sb.Append(" as ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)); ++colidx2;