Skip to content

Commit

Permalink
[2.0.1] Query: Always evalaute Enum.ToString on client
Browse files Browse the repository at this point in the history
Since server would store underlying type of enum hence server eval causes incorrect result

Resolves #9894
  • Loading branch information
smitpatel committed Oct 4, 2017
1 parent cb2a013 commit 71da832
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3966,6 +3966,23 @@ public virtual void Project_collection_navigation_with_inheritance3()
}
}

[ConditionalFact]
public virtual void Enum_ToString_is_client_eval()
{
using (var context = CreateContext())
{
var query = context.Gears
.OrderBy(g => g.SquadId)
.ThenBy(g => g.Nickname)
.Select(g => g.Rank.ToString())
.Take(1)
.ToList();

var result = Assert.Single(query);
Assert.Equal("Corporal", result);
}
}

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

protected GearsOfWarQueryTestBase(TFixture fixture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,15 @@ private static readonly Dictionary<Type, string> _typeMapping
/// </summary>
public virtual Expression Translate(MethodCallExpression methodCallExpression)
{
string storeType;

if (methodCallExpression.Method.Name == nameof(ToString)
&& methodCallExpression.Arguments.Count == 0
&& methodCallExpression.Object != null
&& _typeMapping.TryGetValue(
methodCallExpression.Object.Type
.UnwrapNullableType()
.UnwrapEnumType(),
out storeType))
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue9894", out var enabled)
&& enabled
? methodCallExpression.Object.Type.UnwrapNullableType().UnwrapEnumType()
: methodCallExpression.Object.Type.UnwrapNullableType(),
out var storeType))
{
return new SqlFunctionExpression(
functionName: "CONVERT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3857,6 +3857,19 @@ FROM [Gear] AS [g]
WHERE [g].[Discriminator] IN (N'Officer', N'Gear') AND ((@_outer_Nickname = [g].[LeaderNickname]) AND (@_outer_SquadId = [g].[LeaderSquadId]))");
}

public override void Enum_ToString_is_client_eval()
{
base.Enum_ToString_is_client_eval();

AssertSql(
@"@__p_0='1'
SELECT TOP(@__p_0) [g].[Rank]
FROM [Gear] AS [g]
WHERE [g].[Discriminator] IN (N'Officer', N'Gear')
ORDER BY [g].[SquadId], [g].[Nickname]");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down

0 comments on commit 71da832

Please sign in to comment.