diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/SugarParameter.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/SugarParameter.cs index 639384c88..4715346b4 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/SugarParameter.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/SugarParameter.cs @@ -162,6 +162,16 @@ namespace SqlSugar { this.DbType = System.Data.DbType.UInt16; } + else if (type.Name == "TimeOnly") + { + this.DbType = System.Data.DbType.Time; + this.Value = UtilMethods.TimeOnlyToTimeSpan(this.Value); + } + else if (type.Name == "DateOnly") + { + this.DbType = System.Data.DbType.Date; + this.Value = UtilMethods.DateOnlyToDateTime(this.Value); + } } public SugarParameter(string name, object value, bool isOutput) diff --git a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs index 39440fd2d..6063ff3d6 100644 --- a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs @@ -116,6 +116,19 @@ namespace SqlSugar if (destinationType.IsEnum && value is int) return Enum.ToObject(destinationType, (int)value); + if (destinationType.Name == "TimeOnly"&& sourceType.Name!= "TimeOnly") + { + var type = Type.GetType("System.TimeOnly", true, true); + var method=type.GetMethods().FirstOrDefault(it => it.GetParameters().Length == 1 && it.Name == "FromTimeSpan"); + return method.Invoke(null, new object[] { value }); + } + if (destinationType.Name == "DateOnly" && sourceType.Name != "DateOnly") + { + var type = Type.GetType("System.DateOnly", true, true); + var method = type.GetMethods().FirstOrDefault(it => it.GetParameters().Length == 1 && it.Name == "FromDateTime"); + return method.Invoke(null, new object[] { value }); + } + if (!destinationType.IsInstanceOfType(value)) return Convert.ChangeType(value, destinationType, culture); } @@ -1064,5 +1077,19 @@ namespace SqlSugar { return $"[value=sql{UtilConstants.ReplaceKey}]"; } + + internal static object TimeOnlyToTimeSpan(object value) + { + if (value == null) return null; + var method = value.GetType().GetMethods().First(it => it.GetParameters().Length == 0 && it.Name == "ToTimeSpan"); + return method.Invoke(value, new object[] { }); + } + + internal static object DateOnlyToDateTime(object value) + { + if (value == null) return null; + var method = value.GetType().GetMethods().First(it => it.GetParameters().Length == 0 && it.Name == "ToShortDateString"); + return method.Invoke(value, new object[] { }); + } } }