简体   繁体   中英

Using timestamp with time zone with Npgsql

There is a need to save timestamp with time zone in database. Target field in table is declared as

"UploadTime" timestamp with time zone

I'm inserting data using command parameters :

var uploadTime = DateTimeOffset.Now;
var insertQuery = @"INSERT INTO ""Table"" (""UploadTime"") VALUES :uploadTime"; 
var uploadTimeParam = new NpgsqlParameter("uploadTime", NpgsqlDbType.TimestampTZ);
uploadTimeParam.Value = uploadTime;
var insertCommand = new NpgsqlCommand(insertQuery, databaseConnection);
insertCommand.Parameters.Add(uploadTimeParam);

Value is stored and returned as DateTime without time zone. I guess that's because "timestamp with timezone" database type is mapped to DateTime by default. Probably that can be fixed via checking out sources and making respective changes to TimestampTZ, but it will interfere with idea of using Nuget to manage project dependencies. Maybe there are some less complicated ways to achieve that? Storing ticks and converting them to DateTimeOffset is an overhead, hack with "SET TIME ZONE INTERVAL" before query execution didn't work in my case. Thanks in advance for any solutions.

通过存储其他手动映射到DateTimeOffset的时间戳RTT字段来解决问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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