Skip to content

Commit d4aa1ea

Browse files
authored
ToString() on string columns added (#29349)
Fixes #20839
1 parent 7302231 commit d4aa1ea

File tree

8 files changed

+58
-0
lines changed

8 files changed

+58
-0
lines changed

src/EFCore.SqlServer/Query/Internal/SqlServerObjectToStringTranslator.cs

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public SqlServerObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFact
6767
return null;
6868
}
6969

70+
if(instance.TypeMapping is not null && instance.TypeMapping.ClrType == typeof(string))
71+
{
72+
return instance;
73+
}
74+
7075
if (instance.Type == typeof(bool))
7176
{
7277
if (instance is ColumnExpression columnExpression && columnExpression.IsNullable)

src/EFCore.Sqlite.Core/Query/Internal/SqliteObjectToStringTranslator.cs

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public SqliteObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFactory
6666
return null;
6767
}
6868

69+
if (instance.TypeMapping is not null && instance.TypeMapping.ClrType == typeof(string))
70+
{
71+
return instance;
72+
}
73+
6974
if (instance.Type == typeof(bool))
7075
{
7176
if (instance is ColumnExpression columnExpression && columnExpression.IsNullable)

test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs

+7
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ public virtual Task ToString_guid_property_projection(bool async)
9595
Assert.Equal(e.B.ToLower(), a.B.ToLower());
9696
});
9797

98+
[ConditionalTheory]
99+
[MemberData(nameof(IsAsyncData))]
100+
public virtual Task ToString_string_property_projection(bool async)
101+
=> AssertQuery(
102+
async,
103+
ss => ss.Set<Weapon>().Select(w => w.Name.ToString()));
104+
98105
[ConditionalTheory]
99106
[MemberData(nameof(IsAsyncData))]
100107
public virtual Task ToString_boolean_property_non_nullable(bool async)

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

+9
Original file line numberDiff line numberDiff line change
@@ -4095,6 +4095,15 @@ FROM [Gears] AS [g]
40954095
""");
40964096
}
40974097

4098+
public override async Task ToString_string_property_projection(bool async)
4099+
{
4100+
await base.ToString_string_property_projection(async);
4101+
4102+
AssertSql(@"SELECT [w].[Name]
4103+
FROM [Weapons] AS [w]");
4104+
}
4105+
4106+
40984107
public override async Task ToString_boolean_property_non_nullable(bool async)
40994108
{
41004109
await base.ToString_boolean_property_non_nullable(async);

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

+8
Original file line numberDiff line numberDiff line change
@@ -12495,6 +12495,14 @@ FROM [Weapons] AS [w]
1249512495
""");
1249612496
}
1249712497

12498+
public override async Task ToString_string_property_projection(bool async)
12499+
{
12500+
await base.ToString_string_property_projection(async);
12501+
12502+
AssertSql(@"SELECT [w].[Name]
12503+
FROM [Weapons] AS [w]");
12504+
}
12505+
1249812506
public override async Task ToString_boolean_property_non_nullable(bool async)
1249912507
{
1250012508
await base.ToString_boolean_property_non_nullable(async);

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

+8
Original file line numberDiff line numberDiff line change
@@ -10734,6 +10734,14 @@ FROM [Weapons] AS [w]
1073410734
""");
1073510735
}
1073610736

10737+
public override async Task ToString_string_property_projection(bool async)
10738+
{
10739+
await base.ToString_string_property_projection(async);
10740+
10741+
AssertSql(@"SELECT [w].[Name]
10742+
FROM [Weapons] AS [w]");
10743+
}
10744+
1073710745
public override async Task ToString_boolean_property_non_nullable(bool async)
1073810746
{
1073910747
await base.ToString_boolean_property_non_nullable(async);

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

+8
Original file line numberDiff line numberDiff line change
@@ -9004,6 +9004,14 @@ ELSE NULL
90049004
""");
90059005
}
90069006

9007+
public override async Task ToString_string_property_projection(bool async)
9008+
{
9009+
await base.ToString_string_property_projection(async);
9010+
9011+
AssertSql(@"SELECT [w].[Name]
9012+
FROM [Weapons] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [w]");
9013+
}
9014+
90079015
public override async Task ToString_boolean_property_non_nullable(bool async)
90089016
{
90099017
await base.ToString_boolean_property_non_nullable(async);

test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteTest.cs

+8
Original file line numberDiff line numberDiff line change
@@ -3091,6 +3091,14 @@ SELECT COALESCE(SUM("g"."SquadId"), 0)
30913091
""");
30923092
}
30933093

3094+
public override async Task ToString_string_property_projection(bool async)
3095+
{
3096+
await base.ToString_string_property_projection(async);
3097+
3098+
AssertSql(@"SELECT ""w"".""Name""
3099+
FROM ""Weapons"" AS ""w""");
3100+
}
3101+
30943102
public override async Task ToString_boolean_property_non_nullable(bool async)
30953103
{
30963104
await base.ToString_boolean_property_non_nullable(async);

0 commit comments

Comments
 (0)