From 9f2843e8e30d9c9ff1375a1be403016ac65199a3 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Sun, 3 May 2020 11:12:32 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=20Column(ServerTime=3Dxx?= =?UTF-8?q?x)=20MySql=20=E4=B8=8B=E6=97=A0=E6=B3=95=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E7=B2=BE=E5=BA=A6=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql/Internal/UtilsExpressionTree.cs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index 5ea91584..debd9d0f 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -193,9 +193,25 @@ namespace FreeSql.Internal //} if (colattr.ServerTime != DateTimeKind.Unspecified && new[] { typeof(DateTime), typeof(DateTimeOffset) }.Contains(colattr.MapType.NullableTypeOrThis())) { - col.DbDefaultValue = colattr.ServerTime == DateTimeKind.Local ? common.Now : common.NowUtc; - col.DbInsertValue = colattr.ServerTime == DateTimeKind.Local ? common.Now : common.NowUtc; - col.DbUpdateValue = colattr.ServerTime == DateTimeKind.Local ? common.Now : common.NowUtc; + var commonNow = common.Now; + var commonNowUtc = 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) {