将我的项目升级到 .net 6 现在我在带有 postgreSQL 的 EF Core 中拥有时区

R.H*_*ton 8 c# postgresql entity-framework-core .net-6.0

我刚刚使用 EFCore 和 NPGSQL (Postgres) 将我的 Web api 升级到了 .net6。我必须设置一个开关才能使我的代码正常工作: AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

现在 EFCore 将我的 DateTime 字段创建为“带时区的时间戳”,这破坏了一切。

如何获取日期时间类型的旧“无时区时间戳”

我应该删除 LegacyTimestamp 开关吗?

感谢您的帮助!

R.H*_*ton 17

我通过强制所有 DateTime 和 DateTime 的列类型来修复它?通过在我的 DBContext 的 OnModelCreating 中添加以下代码来“没有时区的时间戳”。

foreach (var property in builder.Model.GetEntityTypes()
                 .SelectMany(t => t.GetProperties())
                 .Where
                 ( p
                   => p.ClrType == typeof(DateTime) 
                      || p.ClrType == typeof(DateTime?)
                 )
        )
{
  property.SetColumnType("timestamp without time zone");
}
Run Code Online (Sandbox Code Playgroud)

感觉就像黑客,但它确实有效。