![](/img/trans.png)
[英].net postgres EF core Cannot write DateTime with Kind=Local to PostgreSQL type 'timestamp with time zone'
[英]Upgraded my project to .net 6 now I have time zone in EF Core with postgreSQL
我刚刚使用 EFCore 和 NPGSQL (Postgres) 将我的 web api 升级到 .net6。 我必须设置一个开关才能使我的代码工作: AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
现在 EFCore 将我的 DateTime 字段创建为“带时区的时间戳” ,这会破坏一切。
如何获取 DateTime 类型的旧“没有时区的时间戳” ?
我应该删除 LegacyTimestamp 开关吗?
谢谢你的帮助!
我通过强制所有 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");
感觉像一个黑客,但它的工作原理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.