Skip to main content

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);

参考文档