Skip to content

ComplexCollection bug on new record creation #37361

@RuslanLatsina

Description

@RuslanLatsina

Bug description

Hi, everyone - I faced the issue probably related to #37026 and got the error related to ComplexCollection .

I use

Microsoft.EntityFrameworkCore                                          10.0.1
Npgsql.EntityFrameworkCore.PostgreSQL                                  10.0.0


Error

The complex entry at the original ordinal '0' for the collection 'Blog.NestedJson.Items' cannot be accessed as the containing entry is in the added state.'

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();


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

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql("<connection string>")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

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

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.0.1

Database provider

Npgsql.EntityFrameworkCore.PostgreSQL

Target framework

NET10

Operating system

No response

IDE

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions