From f1a87fa64b1330c1b25c852a6d4815c21c3143f9 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Thu, 8 Sep 2022 00:18:29 +0200 Subject: [PATCH] Translate DateTime.Date when mapped to PG date Fixes #2499 --- .../NpgsqlDateTimeMemberTranslator.cs | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlDateTimeMemberTranslator.cs b/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlDateTimeMemberTranslator.cs index f32859a55..5b21729b2 100644 --- a/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlDateTimeMemberTranslator.cs +++ b/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlDateTimeMemberTranslator.cs @@ -63,29 +63,34 @@ public NpgsqlDateTimeMemberTranslator( // When given a timestamptz, date_trunc performs the truncation with respect to TimeZone; to avoid that, we use the overload // accepting a time zone, and pass UTC. For regular timestamp (or in legacy timestamp mode), we use the simpler overload without // a time zone. - if (NpgsqlTypeMappingSource.LegacyTimestampBehavior || instance?.TypeMapping is NpgsqlTimestampTypeMapping) + switch (instance?.TypeMapping) { - return _sqlExpressionFactory.Function( - "date_trunc", - new[] { _sqlExpressionFactory.Constant("day"), instance! }, - nullable: true, - argumentsPropagateNullability: TrueArrays[2], - returnType, - instance!.TypeMapping); - } - - if (instance?.TypeMapping is NpgsqlTimestampTzTypeMapping) - { - return _sqlExpressionFactory.Function( - "date_trunc", - new[] { _sqlExpressionFactory.Constant("day"), instance, _sqlExpressionFactory.Constant("UTC") }, - nullable: true, - argumentsPropagateNullability: TrueArrays[3], - returnType, - instance.TypeMapping); + case NpgsqlTimestampTypeMapping: + case NpgsqlTimestampTzTypeMapping when NpgsqlTypeMappingSource.LegacyTimestampBehavior: + return _sqlExpressionFactory.Function( + "date_trunc", + new[] { _sqlExpressionFactory.Constant("day"), instance! }, + nullable: true, + argumentsPropagateNullability: TrueArrays[2], + returnType, + instance.TypeMapping); + + case NpgsqlTimestampTzTypeMapping: + return _sqlExpressionFactory.Function( + "date_trunc", + new[] { _sqlExpressionFactory.Constant("day"), instance, _sqlExpressionFactory.Constant("UTC") }, + nullable: true, + argumentsPropagateNullability: TrueArrays[3], + returnType, + instance.TypeMapping); + + // If DateTime.Date is invoked on a PostgreSQL date, simply no-op. + case NpgsqlDateTypeMapping: + return instance; + + default: + return null; } - - return null; } return member.Name switch