Skip to content

Map two classes with same nullable complex properties to same column will result in a NullReferenceException #37335

Closed

Description

@PoKNicolas

Bug description

I have a base class and several child classes with some of them share some complex properties. I try to map them into one table and want to map same complex properties into the same column. This will result in a NullReferenceException if the property is null. See example code.

With different column names the same setup works.

Your code

using Microsoft.EntityFrameworkCore;

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

context.Products.Add(new Product1() { Name = "Product 1" });
await context.SaveChangesAsync();

context.ChangeTracker.Clear();
var product = await context.Products.FirstAsync();
var originalProduct = (Product)context.ChangeTracker.Entries().First().OriginalValues.ToObject();


public sealed class ProductContext : DbContext
{
    public DbSet<Product> Products { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(Environment.GetEnvironmentVariable("Test__SqlServer__DefaultConnection"))
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>().HasDiscriminator<string>("Discriminator")
            .HasValue<Product1>("Product1").HasValue<Product2>("Product2");
        modelBuilder.Entity<Product1>().ComplexProperty(x => x.Price, p => p.Property(a => a.Amount).HasColumnName("Price"));
        modelBuilder.Entity<Product2>().ComplexProperty(x => x.Price, p => p.Property(a => a.Amount).HasColumnName("Price"));
    }
}

public abstract class Product
{
    public int Id { get; private set; }
    public required string Name { get; set; }
    
}

public class Product1 : Product
{
    public Price? Price { get; set; }
}
public class Product2 : Product
{
    public Price? Price { get; set; }
}

public sealed class Price
{
    public required string Amount { get; init; }
}

Stack traces


Verbose output


EF Core version

10.0.1

Database provider

No response

Target framework

.NET 10

Operating system

No response

IDE

Rider

Activity

  1. changed the title [-]map two nullable complex properties to same column will result in a NullReferenceException[/-] [+]map two classes with same nullable complex properties to same column will result in a NullReferenceException[/+] on Dec 11, 2025
  2. roji commented on Dec 13, 2025

    @roji
    Member

    Confirmed, see exception below. @AndriySvyryd is this scenario supposed to be currently supported?

    Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
       at lambda_method53(Closure, Product1, IReadOnlyList`1, String)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntryBase.WritePropertyValue(IPropertyBase propertyBase, Object value, Boolean forMaterialization)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntryBase.SetProperty(IPropertyBase propertyBase, Object value, Boolean isMaterialization, Boolean setModified, Boolean isCascadeDelete, CurrentValueType valueType)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntryBase.SetProperty(IPropertyBase propertyBase, Object value, Boolean isMaterialization, Boolean setModified, Boolean isCascadeDelete)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntryBase.set_Item(IPropertyBase propertyBase, Object value)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntryBase.AcceptChanges()
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.AcceptChanges()
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.AcceptAllChanges(IReadOnlyList`1 changedEntries)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
       at Program.<Main>$(String[] args) in /Users/roji/projects/test/Program.cs:line 15
       at Program.<Main>$(String[] args) in /Users/roji/projects/test/Program.cs:line 19
       at Program.<Main>(String[] args)
    
  3. added theissue type on Dec 16, 2025
  4. PoKNicolas commented on Jan 13, 2026

    @PoKNicolas
    Author

    Could you give me an idea of when and whether this bug will be fixed? For now, I'll just stick with EF Core 9 and wait for a fix. If no fix is planned in the foreseeable future, I'll have to work on a different mapping.

    By the way, despite the exception, the result appears to be correct in the database.

  5. AndriySvyryd commented on Jan 13, 2026

    @AndriySvyryd
    Member

    It will likely be fixed for EF Core 11, releasing in November, unless we get more reports

  6. added this to the 11.0.0 milestone on Jan 13, 2026
  7. changed the title [-]map two classes with same nullable complex properties to same column will result in a NullReferenceException[/-] [+]Map two classes with same nullable complex properties to same column will result in a NullReferenceException[/+] on Jan 31, 2026
  8. added a commit that references this issue on Feb 27, 2026
    0518708
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions