Skip to content

Commit

Permalink
Query: Assign datetime typemapping to DateTimeOffset.Date
Browse files Browse the repository at this point in the history
Resolves #19052
  • Loading branch information
smitpatel committed Jan 4, 2020
1 parent 6db248e commit 51ca166
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
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

0 comments on commit 51ca166

Please sign in to comment.