EFCore使用PostgreSQL时timestamp特性类型错误
这篇文章内容来自这篇博客 EFCore使用PostgreSQL时,如果有乐观锁特性timestamp,在CodeFirst模式下,生成数据库表可能会报错
Cannot write DateTime with Kind=Local to PostgreSQL type 'timestamp with time zone', only UTC is supported. Note that it's not possible to mix DateTimes with different Kinds in an array/range.
这时需要在调用PG之前的初始化地方(Startup.Class 或 Program.cs)添如下设置:
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
参考文档
- 由于.NET6.0的变更,Npgsql 6.0对时间戳的映射方式进行了一些重要的更改。Date and Time Handling
- AppContext.SetSwitch(String, Boolean) 方法