EFCore Postgres 时间戳失去刻度精度
EFCore Postgres timestamp loses tick precision
我的应用程序在读取 JSON API 时遇到问题,它以下列格式提供 UTC 时间:
2019-09-19T23:46:00.1183275Z
我使用 Entity Framework Core 将这些时间存储在 Postgres 数据库中,稍后使用它们来确保我只从 API 获取更新的数据。我遇到的问题是,当这些时间戳被写入数据库时,我失去了 .NET 对时间戳滴答的精度。 C# 将上述时间戳记为:637045335601183275
,但是当我从数据库中读取同一时间时,记号为 637045335601183270
.
我尝试搜索文档,但找不到更改数据库中时间戳精度的方法。有办法吗?如果失败,是否有一种一致的方法可以将 DateTime
舍入到数据库不会丢失的精度?
示例:
上下文
public class MyContext : DbContext
{
public DbSet<Item> Items { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(@"host=localhost;port=5432;database=TestDb;user id=postgres;password=password");
}
}
项目
public class Item
{
public int Id { get; set; }
public DateTime CreateTime { get; set; }
}
计划
class Program
{
static void Main(string[] args)
{
var item = JsonConvert.DeserializeObject<Item>("{Id: 1, CreateTime: \"2019-09-19T23:46:00.1183275Z\"}");
Item readItem;
using(var context = new MyContext())
{
context.Items.Add(item);
context.SaveChanges();
}
using(var context = new MyContext())
{
readItem = context.Items.First();
}
Console.WriteLine(item.CreateTime.Ticks.Equals(readItem.CreateTime.Ticks)); //Outputs False
}
}
以上使用 EFCore 2.2.6 和 Npgsql.EntityFrameworkCore.PostgreSQL 2.2.4
创建
I tried searching the documentation but I could not find a way to
change the precision of the timestamp in the database. Is there a way?
没有,没有。
可惜 Postgres 日期/时间类型仅准确 to the microsecond(1 / 1,000,000 秒)。
.NET 日期以称为滴答的 100 nanosecond 单位测量(滴答为 1 / 10,000,000 秒)。
由于每微秒有 10 个滴答,.NET 日期将无法通过 Postgres 进行往返。
Failing that is there a consistent way to round the DateTime to a
precision that won't be lost by the database?
是 - DateTime
有一个采用 Ticks
的构造函数。因此,您可以使用 item.CreateTime.Ticks
来获取您的初始刻度值,以某种方式改变它(决定您是想要天花板还是地板还是圆形或其他),然后将它传递给构造函数。这个值(精确到微秒)然后可以通过 Postgres 往返。
我的应用程序在读取 JSON API 时遇到问题,它以下列格式提供 UTC 时间:
2019-09-19T23:46:00.1183275Z
我使用 Entity Framework Core 将这些时间存储在 Postgres 数据库中,稍后使用它们来确保我只从 API 获取更新的数据。我遇到的问题是,当这些时间戳被写入数据库时,我失去了 .NET 对时间戳滴答的精度。 C# 将上述时间戳记为:637045335601183275
,但是当我从数据库中读取同一时间时,记号为 637045335601183270
.
我尝试搜索文档,但找不到更改数据库中时间戳精度的方法。有办法吗?如果失败,是否有一种一致的方法可以将 DateTime
舍入到数据库不会丢失的精度?
示例:
上下文
public class MyContext : DbContext
{
public DbSet<Item> Items { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(@"host=localhost;port=5432;database=TestDb;user id=postgres;password=password");
}
}
项目
public class Item
{
public int Id { get; set; }
public DateTime CreateTime { get; set; }
}
计划
class Program
{
static void Main(string[] args)
{
var item = JsonConvert.DeserializeObject<Item>("{Id: 1, CreateTime: \"2019-09-19T23:46:00.1183275Z\"}");
Item readItem;
using(var context = new MyContext())
{
context.Items.Add(item);
context.SaveChanges();
}
using(var context = new MyContext())
{
readItem = context.Items.First();
}
Console.WriteLine(item.CreateTime.Ticks.Equals(readItem.CreateTime.Ticks)); //Outputs False
}
}
以上使用 EFCore 2.2.6 和 Npgsql.EntityFrameworkCore.PostgreSQL 2.2.4
创建I tried searching the documentation but I could not find a way to change the precision of the timestamp in the database. Is there a way?
没有,没有。
可惜 Postgres 日期/时间类型仅准确 to the microsecond(1 / 1,000,000 秒)。
.NET 日期以称为滴答的 100 nanosecond 单位测量(滴答为 1 / 10,000,000 秒)。
由于每微秒有 10 个滴答,.NET 日期将无法通过 Postgres 进行往返。
Failing that is there a consistent way to round the DateTime to a precision that won't be lost by the database?
是 - DateTime
有一个采用 Ticks
的构造函数。因此,您可以使用 item.CreateTime.Ticks
来获取您的初始刻度值,以某种方式改变它(决定您是想要天花板还是地板还是圆形或其他),然后将它传递给构造函数。这个值(精确到微秒)然后可以通过 Postgres 往返。