From d4aa1ea4946313f59b643d632657ef4e895d96e6 Mon Sep 17 00:00:00 2001 From: bikbov <43091641+bikbov@users.noreply.github.com> Date: Wed, 9 Nov 2022 16:48:28 +0300 Subject: [PATCH] ToString() on string columns added (#29349) Fixes https://github.com/dotnet/efcore/issues/20839 --- .../Query/Internal/SqlServerObjectToStringTranslator.cs | 5 +++++ .../Query/Internal/SqliteObjectToStringTranslator.cs | 5 +++++ .../Query/GearsOfWarQueryTestBase.cs | 7 +++++++ .../Query/GearsOfWarQuerySqlServerTest.cs | 9 +++++++++ .../Query/TPCGearsOfWarQuerySqlServerTest.cs | 8 ++++++++ .../Query/TPTGearsOfWarQuerySqlServerTest.cs | 8 ++++++++ .../Query/TemporalGearsOfWarQuerySqlServerTest.cs | 8 ++++++++ .../Query/GearsOfWarQuerySqliteTest.cs | 8 ++++++++ 8 files changed, 58 insertions(+) diff --git a/src/EFCore.SqlServer/Query/Internal/SqlServerObjectToStringTranslator.cs b/src/EFCore.SqlServer/Query/Internal/SqlServerObjectToStringTranslator.cs index f6362b72ee2..32c288ac643 100644 --- a/src/EFCore.SqlServer/Query/Internal/SqlServerObjectToStringTranslator.cs +++ b/src/EFCore.SqlServer/Query/Internal/SqlServerObjectToStringTranslator.cs @@ -67,6 +67,11 @@ public SqlServerObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFact return null; } + if(instance.TypeMapping is not null && instance.TypeMapping.ClrType == typeof(string)) + { + return instance; + } + if (instance.Type == typeof(bool)) { if (instance is ColumnExpression columnExpression && columnExpression.IsNullable) diff --git a/src/EFCore.Sqlite.Core/Query/Internal/SqliteObjectToStringTranslator.cs b/src/EFCore.Sqlite.Core/Query/Internal/SqliteObjectToStringTranslator.cs index 4c4821a0cc9..3583c43f1cb 100644 --- a/src/EFCore.Sqlite.Core/Query/Internal/SqliteObjectToStringTranslator.cs +++ b/src/EFCore.Sqlite.Core/Query/Internal/SqliteObjectToStringTranslator.cs @@ -66,6 +66,11 @@ public SqliteObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFactory return null; } + if (instance.TypeMapping is not null && instance.TypeMapping.ClrType == typeof(string)) + { + return instance; + } + if (instance.Type == typeof(bool)) { if (instance is ColumnExpression columnExpression && columnExpression.IsNullable) diff --git a/test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs index da1eb9b0884..44c10730303 100644 --- a/test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs @@ -95,6 +95,13 @@ public virtual Task ToString_guid_property_projection(bool async) Assert.Equal(e.B.ToLower(), a.B.ToLower()); }); + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public virtual Task ToString_string_property_projection(bool async) + => AssertQuery( + async, + ss => ss.Set().Select(w => w.Name.ToString())); + [ConditionalTheory] [MemberData(nameof(IsAsyncData))] public virtual Task ToString_boolean_property_non_nullable(bool async) diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerTest.cs index 43027223b73..3d1bb510959 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerTest.cs @@ -4095,6 +4095,15 @@ FROM [Gears] AS [g] """); } + public override async Task ToString_string_property_projection(bool async) + { + await base.ToString_string_property_projection(async); + + AssertSql(@"SELECT [w].[Name] +FROM [Weapons] AS [w]"); + } + + public override async Task ToString_boolean_property_non_nullable(bool async) { await base.ToString_boolean_property_non_nullable(async); diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/TPCGearsOfWarQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/TPCGearsOfWarQuerySqlServerTest.cs index 066af0816d7..12020fe5257 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/TPCGearsOfWarQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/TPCGearsOfWarQuerySqlServerTest.cs @@ -12495,6 +12495,14 @@ FROM [Weapons] AS [w] """); } + public override async Task ToString_string_property_projection(bool async) + { + await base.ToString_string_property_projection(async); + + AssertSql(@"SELECT [w].[Name] +FROM [Weapons] AS [w]"); + } + public override async Task ToString_boolean_property_non_nullable(bool async) { await base.ToString_boolean_property_non_nullable(async); diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/TPTGearsOfWarQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/TPTGearsOfWarQuerySqlServerTest.cs index 34127451719..4322839499a 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/TPTGearsOfWarQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/TPTGearsOfWarQuerySqlServerTest.cs @@ -10734,6 +10734,14 @@ FROM [Weapons] AS [w] """); } + public override async Task ToString_string_property_projection(bool async) + { + await base.ToString_string_property_projection(async); + + AssertSql(@"SELECT [w].[Name] +FROM [Weapons] AS [w]"); + } + public override async Task ToString_boolean_property_non_nullable(bool async) { await base.ToString_boolean_property_non_nullable(async); diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/TemporalGearsOfWarQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/TemporalGearsOfWarQuerySqlServerTest.cs index 3ec6e986606..f6eb7d67917 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/TemporalGearsOfWarQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/TemporalGearsOfWarQuerySqlServerTest.cs @@ -9004,6 +9004,14 @@ ELSE NULL """); } + public override async Task ToString_string_property_projection(bool async) + { + await base.ToString_string_property_projection(async); + + AssertSql(@"SELECT [w].[Name] +FROM [Weapons] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [w]"); + } + public override async Task ToString_boolean_property_non_nullable(bool async) { await base.ToString_boolean_property_non_nullable(async); diff --git a/test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteTest.cs index 1663d900962..6f3832eb387 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteTest.cs @@ -3091,6 +3091,14 @@ SELECT COALESCE(SUM("g"."SquadId"), 0) """); } + public override async Task ToString_string_property_projection(bool async) + { + await base.ToString_string_property_projection(async); + + AssertSql(@"SELECT ""w"".""Name"" +FROM ""Weapons"" AS ""w"""); + } + public override async Task ToString_boolean_property_non_nullable(bool async) { await base.ToString_boolean_property_non_nullable(async);