Skip to content

Complex type columns are non-nullable in TPH when defined on derived entity type #31378

Closed

Description

@roji

When defining a complex property on a derived TPH entity type, its columns are non-nullable, making it impossible to insert other types in the hierarchy.

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

ctx.Set<Blog>().Add(new Blog());
await ctx.SaveChangesAsync();

public class BlogContext : DbContext
{
    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>();
        modelBuilder.Entity<SpecialBlog>().ComplexProperty(s => s.ComplexThing);
    }
}

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

public class SpecialBlog : Blog
{
    public int RegularColumn { get; set; }
    public ComplexThing ComplexThing { get; set; }
}

public class ComplexThing
{
    public int Foo { get; set; }
}

Activity

  1. added this to the 8.0.0 milestone on Aug 2, 2023
  2. added a commit that references this issue on Sep 6, 2023
    6d6e911
  3. removed their assignment
    on Sep 6, 2023
  4. added a commit that references this issue on Sep 7, 2023
    a9d8652
  5. modified the milestones: 8.0.0, on Sep 8, 2023
  6. modified the milestones: , 8.0.0 on Nov 14, 2023
  7. added theissue type on Jun 15, 2025
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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions