diff --git a/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlJsonPocoTranslator.cs b/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlJsonPocoTranslator.cs index 6f2ab72252..35be7f0be9 100644 --- a/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlJsonPocoTranslator.cs +++ b/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlJsonPocoTranslator.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Reflection; +using System.Text.Json.Serialization; using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Query; using Microsoft.EntityFrameworkCore.Query.SqlExpressions; @@ -28,7 +29,16 @@ public NpgsqlJsonPocoTranslator( } public virtual SqlExpression Translate(SqlExpression instance, MemberInfo member, Type returnType) - => TranslateMemberAccess(instance, _sqlExpressionFactory.Constant(member.Name), returnType); + => instance?.TypeMapping is NpgsqlJsonTypeMapping || instance is PostgresJsonTraversalExpression + ? TranslateMemberAccess( + instance, + _sqlExpressionFactory.Constant( + member.GetCustomAttribute() is JsonPropertyNameAttribute + attribute + ? attribute.Name + : member.Name), + returnType) + : null; public virtual SqlExpression TranslateMemberAccess( [NotNull] SqlExpression instance, [NotNull] SqlExpression member, [NotNull] Type returnType) diff --git a/test/EFCore.PG.FunctionalTests/Query/JsonPocoQueryTest.cs b/test/EFCore.PG.FunctionalTests/Query/JsonPocoQueryTest.cs index b88c58bad2..53ac1ac2cd 100644 --- a/test/EFCore.PG.FunctionalTests/Query/JsonPocoQueryTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/JsonPocoQueryTest.cs @@ -2,6 +2,7 @@ using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text.Json; +using System.Text.Json.Serialization; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.TestUtilities; using Npgsql.EntityFrameworkCore.PostgreSQL.TestUtilities; @@ -70,7 +71,7 @@ public void Literal() AssertSql( @"SELECT j.""Id"", j.""Customer"", j.""ToplevelArray"" FROM ""JsonbEntities"" AS j -WHERE j.""Customer"" = '{""Name"":""Test customer"",""Age"":80,""ID"":""00000000-0000-0000-0000-000000000000"",""IsVip"":false,""Statistics"":null,""Orders"":null}'"); +WHERE j.""Customer"" = '{""Name"":""Test customer"",""Age"":80,""ID"":""00000000-0000-0000-0000-000000000000"",""is_vip"":false,""Statistics"":null,""Orders"":null}'"); } [Fact] @@ -163,7 +164,7 @@ public void Bool_output() AssertSql( @"SELECT j.""Id"", j.""Customer"", j.""ToplevelArray"" FROM ""JsonbEntities"" AS j -WHERE CAST(j.""Customer""->>'IsVip' AS boolean) +WHERE CAST(j.""Customer""->>'is_vip' AS boolean) LIMIT 2"); } @@ -639,6 +640,7 @@ public class Customer public string Name { get; set; } public int Age { get; set; } public Guid ID { get; set; } + [JsonPropertyName("is_vip")] public bool IsVip { get; set; } public Statistics Statistics { get; set; } public Order[] Orders { get; set; }