简体   繁体   English

C#Dapper-这个MySql有什么问题?

[英]C# Dapper - What is wrong with this MySql?

I created a database table using this line: 我使用此行创建了一个数据库表:

CREATE TABLE mytable(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
uniquename VARCHAR(256) NOT NULL UNIQUE KEY, creationtime TIMESTAMP, 
updatedtime TIMESTAMP);

Here is my dapper-dot-net INSERT OR UPDATE command: 这是我的dapper-dot-net INSERT或UPDATE命令:

const string uniquename = "25975B8F882E7B1DD99116B71C5A8D04";

// Has format "yyyy-MM-dd HH:mm:ss"
string mysqlTimeStampString = DateTime.UtcNow.ToMysqlTimeStampString();

dbConn.Execute(@"INSERT INTO mytable (uniquename, creationtime, 
updatedtime) VALUES (@uniquename, @creationtime, @updatedtime) ON DUPLICATE 
KEY UPDATE updatedtime=@updatedtime;", new { uniquename=uniquename, 
creationtime = mysqlTimeStampString, updatedtime = mysqlTimeStampString });

After the first time I ran it, I select * from mytable \\G and I got this result: 第一次运行它后,我select * from mytable \\G并得到以下结果:

id: 1
uniquename: 25975B8F882E7B1DD99116B71C5A8D04
creationtime: 2016-01-25 00:06:55
updatedtime: 2016-01-25 00:06:55

So far everything looks good, but when I run the same INSERT OR UPDATE a few minutes later, I get this result: 到目前为止,一切看起来都不错,但是几分钟后我运行相同的INSERT或UPDATE时,得到以下结果:

id: 1
uniquename: 25975B8F882E7B1DD99116B71C5A8D04
creationtime: 2016-01-24 19:10:00
updatedtime: 2016-01-25 00:10:00

This is puzzling for the following reasons: 令人困惑的原因如下:

  1. On duplicate, only the updatedtime field is supposed to be updated, but both the creationtime and duplicatetime fields are being updated. 在重复项上,应该只更新updatedtime字段,但是creationtimeduplicatetime字段都在更新。 Why are both fields being updated? 为什么两个字段都被更新?
  2. The mysqlTimeStampString string is passed into the INSERT OR UPDATE command twice. mysqlTimeStampString字符串两次被传递到INSERT OR UPDATE命令中。 Literally one string passed in twice, so there is absolutely no possibility that creationtime is local time while updatedtime is UTC. 从字面上看一个字符串中两次通过,所以是绝对没有可能性creationtime是本地的时间,而updatedtime是UTC。 How on earth is it possible, during the duplicate update, that the creationtime is being converted to local time (UTC -5:00) while the updatedtime is set to UTC? 如何在地球上是可行的,重复的更新过程中,该creationtime被转换为本地时间(UTC -5:00),而updatedtime设置为UTC?

The best I can figure, this must be a dapper bug, mysql bug, or both, or my syntax is wrong. 据我所知,这一定是dapper错误,mysql错误或两者,否则我的语法是错误的。

  1. In the create table statement creationtime timestamp firld is defined first. 在CREATE TABLE语句creationtime时间戳firld首先被定义。 According to mysql's documentation on auto initialising timestamp fields : 根据mysql的有关自动初始化时间戳字段的文档:

By default, the first TIMESTAMP column has both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP if neither is specified explicitly. 默认情况下,第一个TIMESTAMP列同时具有DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP(如果未明确指定两者)。

  1. Since it is mysql and not the c# code that updates creationtime field, its value is set according to mysql server's clock and timezone setting. 由于是mysql而不是更新creationtime字段的c#代码,因此其值是根据mysql服务器的时钟和时区设置来设置的。

The documentation I linked in the 1st point also describes how to override these settings so that creationtime field does not get updated by mysql. 我在第一点链接的文档还描述了如何覆盖这些设置,以使mysql不会更新creationtime字段。 The solutions are described right below the quoted paragraph. 解决方案在引用的段落下面进行描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM