Support DateOnly TimeOnly

This commit is contained in:
sunkaixuan 2022-12-22 17:29:29 +08:00
parent 60363db476
commit 6afc6180e2
2 changed files with 37 additions and 0 deletions

View File

@ -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)

View File

@ -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[] { });
}
}
}