Skip to content

Commit 9000196

Browse files
committed
Fix type mapping for GETDATE and GETUTCDATE in SqlServer.
1 parent a1e1d46 commit 9000196

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/EFCore.SqlServer/Query/Internal/Translators/SqlServerDateTimeMemberTranslator.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,19 @@ public class SqlServerDateTimeMemberTranslator(
6969
returnType),
7070

7171
nameof(DateTime.Now)
72+
when declaringType == typeof(DateTime)
7273
=> sqlExpressionFactory.Function(
73-
declaringType == typeof(DateTime) ? "GETDATE" : "SYSDATETIMEOFFSET",
74+
"GETDATE",
75+
arguments: [],
76+
nullable: false,
77+
argumentsPropagateNullability: [],
78+
returnType,
79+
typeMappingSource.FindMapping(typeof(DateTime), "datetime")),
80+
81+
nameof(DateTimeOffset.Now)
82+
when declaringType == typeof(DateTimeOffset)
83+
=> sqlExpressionFactory.Function(
84+
"SYSDATETIMEOFFSET",
7485
arguments: [],
7586
nullable: false,
7687
argumentsPropagateNullability: [],
@@ -83,9 +94,10 @@ public class SqlServerDateTimeMemberTranslator(
8394
arguments: [],
8495
nullable: false,
8596
argumentsPropagateNullability: [],
86-
returnType),
97+
returnType,
98+
typeMappingSource.FindMapping(typeof(DateTime), "datetime")),
8799

88-
nameof(DateTime.UtcNow)
100+
nameof(DateTimeOffset.UtcNow)
89101
when declaringType == typeof(DateTimeOffset)
90102
=> sqlExpressionFactory.Convert(
91103
sqlExpressionFactory.Function(

test/EFCore.SqlServer.FunctionalTests/Query/NorthwindWhereQuerySqlServerTest.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.EntityFrameworkCore.TestModels.Northwind;
5+
46
namespace Microsoft.EntityFrameworkCore.Query;
57

68
#nullable disable
@@ -3041,6 +3043,36 @@ ORDER BY [c].[ContactTitle]
30413043

30423044
#endregion Evaluation order of operators
30433045

3046+
[ConditionalTheory, MemberData(nameof(IsAsyncData))]
3047+
public virtual async Task DateTime_Now_has_proper_type_mapping_for_constant_comparison(bool async)
3048+
{
3049+
await AssertQuery(
3050+
async,
3051+
ss => ss.Set<Customer>().Where(x => DateTime.Now > new DateTime(2025, 1, 1)));
3052+
3053+
AssertSql(
3054+
"""
3055+
SELECT [e].[Id]
3056+
FROM [Entities] AS [e]
3057+
WHERE GETDATE() > '2025-01-01T00:00:00.000'
3058+
""");
3059+
}
3060+
3061+
[ConditionalTheory, MemberData(nameof(IsAsyncData))]
3062+
public virtual async Task DateTime_UtcNow_has_proper_type_mapping_for_constant_comparison(bool async)
3063+
{
3064+
await AssertQuery(
3065+
async,
3066+
ss => ss.Set<Customer>().Where(x => DateTime.UtcNow > new DateTime(2025, 1, 1)));
3067+
3068+
AssertSql(
3069+
"""
3070+
SELECT [e].[Id]
3071+
FROM [Entities] AS [e]
3072+
WHERE GETUTCDATE() > '2025-01-01T00:00:00.000'
3073+
""");
3074+
}
3075+
30443076
private void AssertSql(params string[] expected)
30453077
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
30463078

test/EFCore.SqlServer.FunctionalTests/Query/Translations/Temporal/DateTimeTranslationsSqlServerTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public override async Task Now()
1818

1919
AssertSql(
2020
"""
21-
@myDatetime='2015-04-10T00:00:00.0000000'
21+
@myDatetime='2015-04-10T00:00:00.0000000' (DbType = DateTime)
2222
2323
SELECT [b].[Id], [b].[Bool], [b].[Byte], [b].[ByteArray], [b].[DateOnly], [b].[DateTime], [b].[DateTimeOffset], [b].[Decimal], [b].[Double], [b].[Enum], [b].[FlagsEnum], [b].[Float], [b].[Guid], [b].[Int], [b].[Long], [b].[Short], [b].[String], [b].[TimeOnly], [b].[TimeSpan]
2424
FROM [BasicTypesEntities] AS [b]
@@ -32,7 +32,7 @@ public override async Task UtcNow()
3232

3333
AssertSql(
3434
"""
35-
@myDatetime='2015-04-10T00:00:00.0000000'
35+
@myDatetime='2015-04-10T00:00:00.0000000' (DbType = DateTime)
3636
3737
SELECT [b].[Id], [b].[Bool], [b].[Byte], [b].[ByteArray], [b].[DateOnly], [b].[DateTime], [b].[DateTimeOffset], [b].[Decimal], [b].[Double], [b].[Enum], [b].[FlagsEnum], [b].[Float], [b].[Guid], [b].[Int], [b].[Long], [b].[Short], [b].[String], [b].[TimeOnly], [b].[TimeSpan]
3838
FROM [BasicTypesEntities] AS [b]

0 commit comments

Comments
 (0)