Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: Assign datetime typemapping to DateTimeOffset.Date #19479

Merged
merged 1 commit into from
Jan 4, 2020
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 @@ -60,7 +60,9 @@ public virtual SqlExpression Translate(SqlExpression instance, MemberInfo member
"CONVERT",
new[] { _sqlExpressionFactory.Fragment("date"), instance },
returnType,
instance.TypeMapping);
declaringType == typeof(DateTime)
? instance.TypeMapping
: _sqlExpressionFactory.FindMapping(typeof(DateTime)));

case nameof(DateTime.TimeOfDay):
return _sqlExpressionFactory.Convert(instance, returnType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public virtual SqlExpression Translate(SqlExpression instance, MethodInfo method

if (_methodInfoDatePartMapping.TryGetValue(method, out var datePart))
{
// DateAdd does not accept number argument outside of int range
// AddYears/AddMonths take int argument so no need to check for range
return !datePart.Equals("year")
&& !datePart.Equals("month")
&& arguments[0] is SqlConstantExpression sqlConstant
Expand Down
11 changes: 11 additions & 0 deletions test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7407,6 +7407,17 @@ public virtual void Byte_array_filter_by_length_parameter_compiled()
Assert.Equal(2, query(context, byteQueryParam));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task DateTimeOffset_Date_returns_datetime(bool async)
{
var dateTimeOffset = new DateTimeOffset(2, 3, 1, 8, 0, 0, new TimeSpan(-5, 0, 0));

return AssertQuery(
async,
ss => ss.Set<Mission>().Where(m => m.Timeline.Date >= dateTimeOffset.Date));
}

protected GearsOfWarContext CreateContext() => Fixture.CreateContext();

protected virtual void ClearLog()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7400,6 +7400,18 @@ FROM [Weapons] AS [w]
ORDER BY [w0].[IsAutomatic]");
}

public override async Task DateTimeOffset_Date_returns_datetime(bool async)
{
await base.DateTimeOffset_Date_returns_datetime(async);

AssertSql(
@"@__dateTimeOffset_Date_0='0002-03-01T00:00:00'

SELECT [m].[Id], [m].[CodeName], [m].[Rating], [m].[Timeline]
FROM [Missions] AS [m]
WHERE CONVERT(date, [m].[Timeline]) >= @__dateTimeOffset_Date_0");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public override Task Where_datetimeoffset_year_component(bool async)
public override Task DateTimeOffset_Contains_Less_than_Greater_than(bool async)
=> AssertTranslationFailed(() => base.DateTimeOffset_Contains_Less_than_Greater_than(async));

public override Task DateTimeOffset_Date_returns_datetime(bool async)
=> AssertTranslationFailed(() => base.DateTimeOffset_Date_returns_datetime(async));

// Sqlite does not support cross/outer apply
public override Task Correlated_collections_inner_subquery_predicate_references_outer_qsre(bool async) => null;

Expand Down