Skip to content

Commit 7e31762

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

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

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

Lines changed: 14 additions & 2 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(DateTime.Now)
82+
when declaringType == typeof(DateTimeOffset)
83+
=> sqlExpressionFactory.Function(
84+
"SYSDATETIMEOFFSET",
7485
arguments: [],
7586
nullable: false,
7687
argumentsPropagateNullability: [],
@@ -83,7 +94,8 @@ public class SqlServerDateTimeMemberTranslator(
8394
arguments: [],
8495
nullable: false,
8596
argumentsPropagateNullability: [],
86-
returnType),
97+
returnType,
98+
typeMappingSource.FindMapping(typeof(DateTime), "datetime")),
8799

88100
nameof(DateTime.UtcNow)
89101
when declaringType == typeof(DateTimeOffset)

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

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ protected class ReproEntity13118
586586

587587
#region 14095
588588

589-
[ConditionalTheory, InlineData(false), InlineData(true)]
589+
[ConditionalTheory, MemberData(nameof(IsAsyncData))]
590590
public async Task Where_equals_DateTime_Now(bool async)
591591
{
592592
var contextFactory = await InitializeAsync<Context14095>(seed: c => c.SeedAsync());
@@ -611,7 +611,7 @@ FROM [Dates] AS [d]
611611
""");
612612
}
613613

614-
[ConditionalTheory, InlineData(false), InlineData(true)]
614+
[ConditionalTheory, MemberData(nameof(IsAsyncData))]
615615
public async Task Where_not_equals_DateTime_Now(bool async)
616616
{
617617
var contextFactory = await InitializeAsync<Context14095>(seed: c => c.SeedAsync());
@@ -636,7 +636,7 @@ FROM [Dates] AS [d]
636636
""");
637637
}
638638

639-
[ConditionalTheory, InlineData(false), InlineData(true)]
639+
[ConditionalTheory, MemberData(nameof(IsAsyncData))]
640640
public async Task Where_equals_new_DateTime(bool async)
641641
{
642642
var contextFactory = await InitializeAsync<Context14095>(seed: c => c.SeedAsync());
@@ -668,7 +668,7 @@ FROM [Dates] AS [d]
668668
""");
669669
}
670670

671-
[ConditionalTheory, InlineData(false), InlineData(true)]
671+
[ConditionalTheory, MemberData(nameof(IsAsyncData))]
672672
public async Task Where_contains_DateTime_literals(bool async)
673673
{
674674
var dateTimes = new[]
@@ -1003,7 +1003,7 @@ public class DatesAndPrunes14095
10031003

10041004
#region 15518
10051005

1006-
[ConditionalTheory, InlineData(false), InlineData(true)]
1006+
[ConditionalTheory, MemberData(nameof(IsAsyncData))]
10071007
public virtual async Task Nested_queries_does_not_cause_concurrency_exception_sync(bool tracking)
10081008
{
10091009
var contextFactory = await InitializeAsync<Context15518>(seed: c => c.SeedAsync());
@@ -2620,4 +2620,58 @@ FROM [TestEntities] AS [t]
26202620
""");
26212621
}
26222622
}
2623+
2624+
#region 36479
2625+
2626+
[ConditionalTheory, MemberData(nameof(IsAsyncData))]
2627+
public async Task DateTime_Now_has_proper_type_mapping(bool async)
2628+
{
2629+
var contextFactory = await InitializeAsync<Context36479>();
2630+
using var context = contextFactory.CreateContext();
2631+
2632+
var query = context.Entities.Where(x => DateTime.Now > new DateTime(2025, 1, 1));
2633+
2634+
var result = async
2635+
? await query.ToListAsync()
2636+
: query.ToList();
2637+
2638+
AssertSql(
2639+
"""
2640+
SELECT [e].[Id]
2641+
FROM [Entities] AS [e]
2642+
WHERE GETDATE() > '2025-01-01T00:00:00.000'
2643+
""");
2644+
}
2645+
2646+
[ConditionalTheory, MemberData(nameof(IsAsyncData))]
2647+
public async Task DateTime_UtcNow_has_proper_type_mapping(bool async)
2648+
{
2649+
var contextFactory = await InitializeAsync<Context36479>();
2650+
using var context = contextFactory.CreateContext();
2651+
2652+
var query = context.Entities.Where(x => DateTime.UtcNow > new DateTime(2025, 1, 1));
2653+
2654+
var result = async
2655+
? await query.ToListAsync()
2656+
: query.ToList();
2657+
2658+
AssertSql(
2659+
"""
2660+
SELECT [e].[Id]
2661+
FROM [Entities] AS [e]
2662+
WHERE GETUTCDATE() > '2025-01-01T00:00:00.000'
2663+
""");
2664+
}
2665+
2666+
protected class Context36479(DbContextOptions options) : DbContext(options)
2667+
{
2668+
public DbSet<Entity> Entities { get; set; }
2669+
2670+
public class Entity
2671+
{
2672+
public int Id { get; set; }
2673+
}
2674+
}
2675+
2676+
#endregion
26232677
}

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)