Skip to content

Issue with change tracker not detecting changes on nested complex collections #37026

@mario-mestrovic

Description

@mario-mestrovic

Bug description

While making a workaround for this #37009 and until .NET 10 and EF Core 10 are GA I've found more issues.

Looks like EF can't detect changes in nested complex collections (like Items in this example) (which means SaveChanges won't trigger an UPDATE statement).

Your code

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

var blog = new Blog
{
    Name = "Blog1",
    NestedJson = new NestedJson
    {
        Item = new() { Name = "foo" },
        Items =
            [
                new() { Name = "bar" },
                new() { Name = "baz" },
            ]
    }
};
context.Blogs.Add(blog);
await context.SaveChangesAsync();


blog.NestedJson = blog.NestedJson with
{
    Items = [
                new() { Name = "bar" },
                new() { Name = "baz" },
                new() { Name = "new-bar" },
            ]
};


context.ChangeTracker.DetectChanges();

var modified = context.ChangeTracker.Entries().Any(x=>x.State == EntityState.Modified);
Console.WriteLine($"Is Modified: {modified}"); // Output: Is Modified: False (should be True)

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>().ComplexProperty(e => e.NestedJson, b =>
        {
            b.ToJson();

            b.ComplexProperty(a => a.Item, b =>
            {
                b.Property(x => x.Name).HasJsonPropertyName("name");
            });

            b.ComplexCollection(a => a.Items, b =>
            {
                b.Property(x => x.Name).HasJsonPropertyName("name");
            });
        });
    }
}

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }

    public NestedJson NestedJson { get; set; }
}

public record NestedJson
{
    public NestedItem Item { get; init; }
    public List<NestedItem> Items { get; init; }
}

public record NestedItem
{
    public string Name { get; init; } = string.Empty;
}

Stack traces


Verbose output


EF Core version

10 RC2

Database provider

No response

Target framework

.NET 10 RC2

Operating system

Windows 11

IDE

Visual Studio 2026

Metadata

Metadata

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions