Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,24 @@ public class SqlServerDateTimeMemberTranslator(
returnType),

nameof(DateTime.Now)
when declaringType == typeof(DateTime)
=> sqlExpressionFactory.Function(
declaringType == typeof(DateTime) ? "GETDATE" : "SYSDATETIMEOFFSET",
"GETDATE",
arguments: [],
nullable: false,
argumentsPropagateNullability: [],
returnType),
typeof(DateTime),
typeMappingSource.FindMapping("datetime")),

nameof(DateTimeOffset.Now)
when declaringType == typeof(DateTimeOffset)
=> sqlExpressionFactory.Function(
"SYSDATETIMEOFFSET",
arguments: [],
nullable: false,
argumentsPropagateNullability: [],
typeof(DateTimeOffset),
typeMappingSource.FindMapping("datetimeoffset")),

nameof(DateTime.UtcNow)
when declaringType == typeof(DateTime)
Expand All @@ -83,17 +95,21 @@ public class SqlServerDateTimeMemberTranslator(
arguments: [],
nullable: false,
argumentsPropagateNullability: [],
returnType),
typeof(DateTime),
typeMappingSource.FindMapping("datetime")),

nameof(DateTime.UtcNow)
nameof(DateTimeOffset.UtcNow)
when declaringType == typeof(DateTimeOffset)
=> sqlExpressionFactory.Convert(
sqlExpressionFactory.Function(
"SYSUTCDATETIME",
arguments: [],
nullable: false,
argumentsPropagateNullability: [],
returnType), returnType),
typeof(DateTime),
typeMappingSource.FindMapping("datetime2")),
typeof(DateTimeOffset),
typeMappingSource.FindMapping("datetimeoffset")),

nameof(DateTime.Today)
=> sqlExpressionFactory.Function(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.TestModels.BasicTypesModel;

namespace Microsoft.EntityFrameworkCore.Query.Translations.Temporal;

public class DateTimeOffsetTranslationsSqlServerTest : DateTimeOffsetTranslationsTestBase<BasicTypesQuerySqlServerFixture>
Expand Down Expand Up @@ -298,6 +300,34 @@ FROM [BasicTypesEntities] AS [b]
""");
}

[ConditionalFact]
public virtual async Task Now_has_proper_type_mapping_for_constant_comparison()
{
await AssertQuery(
ss => ss.Set<BasicTypesEntity>().Where(x => DateTimeOffset.Now > new DateTimeOffset(2025, 1, 1, 0, 0, 0, TimeSpan.Zero)));

AssertSql(
"""
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]
FROM [BasicTypesEntities] AS [b]
WHERE SYSDATETIMEOFFSET() > '2025-01-01T00:00:00.0000000+00:00'
""");
}

[ConditionalFact]
public virtual async Task UtcNow_has_proper_type_mapping_for_constant_comparison()
{
await AssertQuery(
ss => ss.Set<BasicTypesEntity>().Where(x => DateTimeOffset.UtcNow > new DateTimeOffset(2025, 1, 1, 0, 0, 0, TimeSpan.Zero)));

AssertSql(
"""
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]
FROM [BasicTypesEntities] AS [b]
WHERE CAST(SYSUTCDATETIME() AS datetimeoffset) > '2025-01-01T00:00:00.0000000+00:00'
""");
}

[ConditionalFact]
public virtual void Check_all_tests_overridden()
=> TestHelpers.AssertAllMethodsOverridden(GetType());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.TestModels.BasicTypesModel;

namespace Microsoft.EntityFrameworkCore.Query.Translations.Temporal;

public class DateTimeTranslationsSqlServerTest : DateTimeTranslationsTestBase<BasicTypesQuerySqlServerFixture>
Expand All @@ -18,7 +20,7 @@ public override async Task Now()

AssertSql(
"""
@myDatetime='2015-04-10T00:00:00.0000000'
@myDatetime='2015-04-10T00:00:00.0000000' (DbType = DateTime)

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]
FROM [BasicTypesEntities] AS [b]
Expand All @@ -32,7 +34,7 @@ public override async Task UtcNow()

AssertSql(
"""
@myDatetime='2015-04-10T00:00:00.0000000'
@myDatetime='2015-04-10T00:00:00.0000000' (DbType = DateTime)

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]
FROM [BasicTypesEntities] AS [b]
Expand Down Expand Up @@ -241,6 +243,34 @@ FROM [BasicTypesEntities] AS [b]
""");
}

[ConditionalFact]
public virtual async Task Now_has_proper_type_mapping_for_constant_comparison()
{
await AssertQuery(
ss => ss.Set<BasicTypesEntity>().Where(x => DateTime.Now > new DateTime(2025, 1, 1)));

AssertSql(
"""
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]
FROM [BasicTypesEntities] AS [b]
WHERE GETDATE() > '2025-01-01T00:00:00.000'
""");
}

[ConditionalFact]
public virtual async Task UtcNow_has_proper_type_mapping_for_constant_comparison()
{
await AssertQuery(
ss => ss.Set<BasicTypesEntity>().Where(x => DateTime.UtcNow > new DateTime(2025, 1, 1)));

AssertSql(
"""
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]
FROM [BasicTypesEntities] AS [b]
WHERE GETUTCDATE() > '2025-01-01T00:00:00.000'
""");
}

[ConditionalFact]
public virtual void Check_all_tests_overridden()
=> TestHelpers.AssertAllMethodsOverridden(GetType());
Expand Down
Loading