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 de55358cda3..e79869659cd 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerTest.cs @@ -3539,6 +3539,15 @@ FROM [Gears] AS [g] ORDER BY [g].[SquadId], [g].[Nickname]"); } + 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 a25705bda19..c1099ef2be5 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/TPCGearsOfWarQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/TPCGearsOfWarQuerySqlServerTest.cs @@ -11307,6 +11307,14 @@ FROM [Weapons] AS [w] ORDER BY [t].[Id], [t2].[Nickname], [t2].[FullName], [t2].[HasSoulPatch]"); } + 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 e286e436332..f1f66013908 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/TPTGearsOfWarQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/TPTGearsOfWarQuerySqlServerTest.cs @@ -9540,6 +9540,14 @@ FROM [Weapons] AS [w] ORDER BY [t].[Id], [t1].[Nickname], [t1].[FullName], [t1].[HasSoulPatch]"); } + 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 60d5bd31726..1c8c9210229 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/TemporalGearsOfWarQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/TemporalGearsOfWarQuerySqlServerTest.cs @@ -7838,6 +7838,14 @@ ELSE NULL CROSS JOIN [Gears] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [g0]"); } + 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 3c767f5ac28..71bd07b903b 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteTest.cs @@ -2731,6 +2731,14 @@ public override async Task Sum_with_optional_navigation_is_translated_to_sql(boo WHERE ""t"".""Note"" <> 'Foo' OR (""t"".""Note"" IS NULL)"); } + 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);