所以我更新到 MySql.Data 8.0.13
并且我注意到我的 TIMESTAMP
字段来自 DateTimeKind.Local
和 DATETIME
字段被读取为 DateTimeKind.Unspecified
我认为“Timestamp”是表示“CreatedTime”字段的绝佳语义类型,所以我选择了它。
问题是,我通常在插入查询中“手动”写入这些日期,它们由应用程序以 UTC 格式生成,以确保本地时间和它们疯狂的时区/夏令时不会破坏任何东西。我不对它们使用 on-update 或 on-insert。现在我遇到了应用程序的 JSON 序列化程序将已经 UTC 时间再次转换为 UTC 的问题,这会导致错误的偏移时间。
所以我的问题是:
有没有办法强制 MySQL.Data “将那些日期/时间戳视为 UTC”?还是我最好停止使用 TIMESTAMP
并坚持使用 DATETIME
?
或者我应该像这样搞一个疯狂的噱头吗?
if(databaseValue is DateTime dt) {
databaseValue = new DateTime(dt.Ticks, DateTimeKind.UTC);
}
哪种方法更适合解决这个问题?
最佳答案
有一个替代库 MySqlConnector其中有一个 DateTimeKind
选项,您可以在 connection string 中指定:
The DateTimeKind used when MySqlDataReader returns a DateTime. If set to Utc or Local, a MySqlException will be thrown if a DateTime command parameter has a Kind of Local or Utc, respectively.
关于MySQL.Data DateTime.Kind 在 SQL DATETIME 和 TIMESTAMP 之间不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53678060/