0

This is not duplicate of .NET6 and DateTime problem. Cannot write DateTime with Kind=UTC to PostgreSQL type 'timestamp without time zone'

My code works

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

If I replace PayableDate = new DateTime(DateTime.Today.Year, 1, 1), with PayableDate = new DateTime(currentYear, 1, 1), code fails with messages

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.

PayableDate type is 'timestamp without timezone' and I am not mixing anything. Trying to set kind in new DateTime to UTC just reverts the message: I cannot save UTC in 'timestamp without timezone'.

Is this a bug in npgsql or is there a subtle difference in DateTime construction

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

that I do not see?

Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16
Milan
  • 969
  • 2
  • 16
  • 34
  • I can't reproduce this, you'll have to submit an actual repro rather than a fragment, a minimal console program should do it (that's always a good idea when submitting questions/issues). – Shay Rojansky Apr 09 '23 at 07:58

0 Answers0