Skip to content

[JsonPropertyName] is ignored for complex properties and complex collections mapped to JSON #39484

Description

@proggmatic

Bug description

When a complex type is mapped to a JSON column with ComplexProperty(...).ToJson(...), the [JsonPropertyName] attribute
is applied to scalar properties inside the complex type, but it is ignored for nested complex properties and complex collections.
They are stored and queried under their CLR names.

With owned types (OwnsOne(...).ToJson(...)) the same attribute is respected for nested owned navigations
(RelationalNavigationJsonPropertyNameAttributeConvention), so migrating existing JSON data from owned types to complex types
silently changes the JSON keys: existing documents are no longer read correctly and new documents are written with different keys.

As far as I can see, there is RelationalPropertyJsonPropertyNameAttributeConvention for properties and
RelationalNavigationJsonPropertyNameAttributeConvention for navigations, but no equivalent convention for complex properties,
although HasJsonPropertyName / SetJsonPropertyName exist for them.

Your code

### Repro


using System.Text.Json.Serialization;
using Microsoft.EntityFrameworkCore;

using var context = new BlogContext();

foreach (var complexProperty in context.Model.FindEntityType(typeof(Blog))!.GetComplexProperties())
    Print(complexProperty.ComplexType, complexProperty.Name);

Console.WriteLine(context.Blogs.Where(b => b.Details.Tags.Any(t => t.Name == "ef")).ToQueryString());

static void Print(Microsoft.EntityFrameworkCore.Metadata.IComplexType type, string path)
{
    foreach (var p in type.GetProperties())
        Console.WriteLine($"{path}.{p.Name} -> \"{p.GetJsonPropertyName()}\"");
    foreach (var c in type.GetComplexProperties())
    {
        Console.WriteLine($"{path}.{c.Name} -> \"{c.GetJsonPropertyName()}\"");
        Print(c.ComplexType, $"{path}.{c.Name}");
    }
}

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs => Set<Blog>();

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseNpgsql("Host=localhost;Database=repro");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
        => modelBuilder.Entity<Blog>().ComplexProperty(b => b.Details, b => b.ToJson("details"));
}

public class Blog
{
    public int Id { get; set; }
    public BlogDetails Details { get; set; } = new();
}

public class BlogDetails
{
    [JsonPropertyName("t")]
    public string? Title { get; set; }

    [JsonPropertyName("a")]
    public Author? Author { get; set; }

    [JsonPropertyName("tg")]
    public List<Tag> Tags { get; set; } = [];
}

public class Author
{
    [JsonPropertyName("n")]
    public string? Name { get; set; }
}

public class Tag
{
    [JsonPropertyName("n")]
    public string Name { get; set; } = "";
}


### Actual output


Details.Title -> "t"
Details.Author -> "Author"
Details.Author.Name -> "n"
Details.Tags -> "Tags"
Details.Tags.Name -> "n"

SELECT b."Id", b.details
FROM "Blogs" AS b
WHERE EXISTS (
    SELECT 1
    FROM ROWS FROM (jsonb_to_recordset(b.details -> 'Tags') AS (n text)) WITH ORDINALITY AS t
    WHERE t.n = 'ef')


### Expected

`Details.Author -> "a"`, `Details.Tags -> "tg"` and `b.details -> 'tg'` in the query, the same as with owned types:
with `OwnsOne(b => b.Details, b => { b.ToJson("details"); b.OwnsOne(d => d.Author); b.OwnsMany(d => d.Tags); })`
the JSON names are `"a"` and `"tg"` and the query uses `b.details -> 'tg'`.

### Workaround

Set the names explicitly after the mapping:


foreach (var complexProperty in modelBuilder.Model.GetEntityTypes().SelectMany(x => x.GetDeclaredComplexProperties()))
    Apply(complexProperty.ComplexType);

static void Apply(IMutableComplexType complexType)
{
    foreach (var complexProperty in complexType.GetComplexProperties())
    {
        if (complexProperty.PropertyInfo?.GetCustomAttribute<JsonPropertyNameAttribute>() is { } attribute)
            complexProperty.SetJsonPropertyName(attribute.Name);

        Apply(complexProperty.ComplexType);
    }
}

Stack traces


Verbose output


EF Core version

10.0.14

Database provider

Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3

Target framework

.NET 10.0

Operating system

Windows 11

IDE

No response

Activity

  1. added
    good first issue
    on Nov 2, 2026
  2. added this to the Backlog milestone on Nov 2, 2026
  3. added a commit that references this issue on Nov 4, 2026
    bf44b93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-complex-typesarea-jsoncommunity-contributioncustomer-reportedgood first issue

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions