From 9eaae319453060b171e90ebcd4149b69cf4ce39f Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 4 Apr 2023 15:45:05 +0800 Subject: [PATCH] CodeFirst (DateOnly TimeOnly) --- .../Abstract/CodeFirstProvider/CodeFirstProvider.cs | 12 +++++++++++- .../Abstract/SqlBuilderProvider/InsertBuilder.cs | 9 ++++++++- .../Abstract/SqlBuilderProvider/UpdateBuilder.cs | 9 ++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index 5a111f10f..57080b95e 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -241,7 +241,17 @@ namespace SqlSugar var tableName = GetTableName(entityInfo); this.Context.MappingTables.Add(entityInfo.EntityName, tableName); entityInfo.DbTableName = tableName; - entityInfo.Columns.ForEach(it => { it.DbTableName = tableName; }); + entityInfo.Columns.ForEach(it => { + it.DbTableName = tableName; + if (it.UnderType?.Name == "DateOnly" && it.DataType == null) + { + it.DataType = "Date"; + } + if (it.UnderType?.Name == "TimeOnly" && it.DataType == null) + { + it.DataType = "Time"; + } + }); var isAny = this.Context.DbMaintenance.IsAnyTable(tableName, false); if (isAny && entityInfo.IsDisabledUpdateAll) { diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs index 5afc651e4..73af85dd9 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs @@ -319,7 +319,14 @@ namespace SqlSugar { var timeSpan = UtilMethods.DateOnlyToDateTime(columnInfo.Value); var pname = Builder.SqlParameterKeyWord + columnInfo.DbColumnName + "_ts" + GetDbColumnIndex; - this.Parameters.Add(new SugarParameter(pname,Convert.ToDateTime(timeSpan))); + if (timeSpan == null) + { + this.Parameters.Add(new SugarParameter(pname, null) { DbType=System.Data.DbType.Date }); + } + else + { + this.Parameters.Add(new SugarParameter(pname, Convert.ToDateTime(timeSpan))); + } GetDbColumnIndex++; return pname; } diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs index e41a470ee..84fa98a6b 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs @@ -417,7 +417,14 @@ namespace SqlSugar { var timeSpan = UtilMethods.TimeOnlyToTimeSpan(columnInfo.Value); var pname = Builder.SqlParameterKeyWord + columnInfo.DbColumnName + "_ts" + GetDbColumnIndex; - this.Parameters.Add(new SugarParameter(pname, timeSpan)); + if (timeSpan == null) + { + this.Parameters.Add(new SugarParameter(pname, null) { DbType = System.Data.DbType.Date }); + } + else + { + this.Parameters.Add(new SugarParameter(pname, timeSpan)); + } GetDbColumnIndex++; return pname; }