PostgreSQL在.NET 6.0使用DateTime类型抛出异常:Cannot write DateTime with Kind=Local to PostgreSQL type ‘timestamp.….

这几天一直在做版本升级,具体是把.netcore 2.2升级到.net6.0。在升级的过程中Run的时候出现了一个错误,具体错误代码如下:

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. See the Npgsql.EnableLegacyTimestampBehavior AppContext switch to enable legacy behavior

看起来,好像已经告诉我们答案了,意思是让我们通过 AppContext 启用以前(传统)的行为 — Npgsql.EnableLegacyTimestampBehavior。通过搜索关键词 EnableLegacyTimestampBehavior,我找到官网的解释:https://www.npgsql.org/doc/types/datetime.html#timestamps-and-timezones

找了一番下来,在Program.cs中添加相应配置就行了。

 AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
 AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);

为您推荐

发表评论

您的电子邮箱地址不会被公开。