- 修复 Column(ServerTime=xxx) MySql 下无法保留精度的问题;

This commit is contained in:
28810 2020-05-03 11:12:32 +08:00
parent 9b77dfe548
commit 9f2843e8e3

View File

@ -193,9 +193,25 @@ namespace FreeSql.Internal
//} //}
if (colattr.ServerTime != DateTimeKind.Unspecified && new[] { typeof(DateTime), typeof(DateTimeOffset) }.Contains(colattr.MapType.NullableTypeOrThis())) if (colattr.ServerTime != DateTimeKind.Unspecified && new[] { typeof(DateTime), typeof(DateTimeOffset) }.Contains(colattr.MapType.NullableTypeOrThis()))
{ {
col.DbDefaultValue = colattr.ServerTime == DateTimeKind.Local ? common.Now : common.NowUtc; var commonNow = common.Now;
col.DbInsertValue = colattr.ServerTime == DateTimeKind.Local ? common.Now : common.NowUtc; var commonNowUtc = common.NowUtc;
col.DbUpdateValue = colattr.ServerTime == DateTimeKind.Local ? common.Now : common.NowUtc; switch (common._orm.Ado.DataType)
{
case DataType.MySql:
case DataType.OdbcMySql: //处理毫秒
var timeLength = 0;
var mTimeLength = Regex.Match(colattr.DbType, @"(DATETIME|TIMESTAMP)\s*\((\d+)\)");
if (mTimeLength.Success) timeLength = int.Parse(mTimeLength.Groups[2].Value);
if (timeLength > 0 && timeLength < 7)
{
commonNow = $"{commonNow.TrimEnd('(', ')')}({timeLength})";
commonNowUtc = $"{commonNowUtc.TrimEnd('(', ')')}({timeLength})";
}
break;
}
col.DbDefaultValue = colattr.ServerTime == DateTimeKind.Local ? commonNow : commonNowUtc;
col.DbInsertValue = colattr.ServerTime == DateTimeKind.Local ? commonNow : commonNowUtc;
col.DbUpdateValue = colattr.ServerTime == DateTimeKind.Local ? commonNow : commonNowUtc;
} }
if (string.IsNullOrEmpty(colattr.InsertValueSql) == false) if (string.IsNullOrEmpty(colattr.InsertValueSql) == false)
{ {