Skip to content

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

@roji

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; }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions