npgsql:看似相同的新 DateTime 构造的不同处理

问题描述 投票:0回答:0

这不是 .NET6 和 DateTime 问题的重复。无法将 Kind=UTC 的 DateTime 写入 PostgreSQL 类型“没有时区的时间戳”

我的代码有效

var currentYear = DateTime.Today.Year;

var membershipPayments = from p in _context.Payments
                         ... joins ...
                         select new Payment
                         {
                             ...
                             PayableDate = new DateTime(DateTime.Today.Year, 1, 1),
                             ...
                         };
_context.AddRange(membershipPayments);

如果我将

PayableDate = new DateTime(DateTime.Today.Year, 1, 1),
替换为
PayableDate = new DateTime(currentYear, 1, 1),
代码将失败并显示消息

无法将 Kind=Local 的 DateTime 写入 PostgreSQL type 'timestamp with time zone',仅支持 UTC。请注意,不可能在数组/范围中混合使用具有不同种类的日期时间。请参阅 Npgsql.EnableLegacyTimestampBehavior AppContext 开关以启用旧版行为。

PayableDate 类型是“没有时区的时间戳”,我没有混合任何东西。尝试将新 DateTime 中的种类设置为 UTC 只会恢复消息:我无法将 UTC 保存在“没有时区的时间戳”中。

这是 npgsql 中的错误还是 DateTime 构造中存在细微差别

  • new DateTime(DateTime.Today.Year, 1, 1)
  • new DateTime(currentYear, 1, 1)

我没看到?

c# entity-framework-core npgsql
© www.soinside.com 2019 - 2024. All rights reserved.