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)
vsnew DateTime(currentYear, 1, 1)
that I do not see?