[英]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: 令人困惑的原因如下:
updatedtime
field is supposed to be updated, but both the creationtime
and duplicatetime
fields are being updated. updatedtime
字段,但是creationtime
和duplicatetime
字段都在更新。 Why are both fields being updated? 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错误或两者,否则我的语法是错误的。
creationtime
timestamp firld is defined first. creationtime
时间戳firld首先被定义。 According to mysql's documentation on auto initialising timestamp fields : 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(如果未明确指定两者)。
creationtime
field, its value is set according to mysql server's clock and timezone setting. 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.