Skip to content

JSON complex property on an optional table-sharing dependent: column is NOT NULL and not cleared on delete #39445

Closed

Description

@jeremy-morren

Bug description

  1. Inserting a Car without Details fails: Cannot insert the value NULL into column 'Specs'. The column nullability only accounts for TPH (Complex property stored as json will be marked non-nullable even in TPH class hierarchy #37404), not optional dependents.
  2. Removing Details updates Color to NULL but leaves the old JSON in Specs.

Your code

public class Car
{
    public int Id { get; set; }
    public string Name { get; set; } = null!;
    public CarDetails? Details { get; set; }
}

public class CarDetails
{
    public int Id { get; set; }
    public string Color { get; set; } = null!;
    public CarSpecs Specs { get; set; } = null!;
}

public class CarSpecs
{
    public int Doors { get; set; }
}

modelBuilder.Entity<Car>()
    .ToTable("Cars")
    .HasOne(c => c.Details)
    .WithOne()
    .HasForeignKey<CarDetails>(d => d.Id);

modelBuilder.Entity<CarDetails>(b =>
{
    b.ToTable("Cars");
    b.ComplexProperty(d => d.Specs)
        .ToJson();
});

EF Core version

10.0.0 and later (since complex ToJson() was added in 10.0.0-preview.7). Not a regression.

Activity

  1. added theissue type on Oct 29, 2026
  2. added this to the 12.0-preview1 milestone on Oct 29, 2026
  3. added a commit that references this issue on Oct 30, 2026
    112ce0c
  4. added a commit that references this issue on Oct 30, 2026
    f6a2140
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions