You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Map two classes with same nullable complex properties to same column will result in a NullReferenceException #37335
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
usingMicrosoft.EntityFrameworkCore;awaitusingvarcontext=newProductContext();awaitcontext.Database.EnsureDeletedAsync();awaitcontext.Database.EnsureCreatedAsync();context.Products.Add(newProduct1(){Name="Product 1"});awaitcontext.SaveChangesAsync();context.ChangeTracker.Clear();varproduct=awaitcontext.Products.FirstAsync();varoriginalProduct=(Product)context.ChangeTracker.Entries().First().OriginalValues.ToObject();publicsealedclassProductContext:DbContext{publicDbSet<Product>Products{get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseSqlServer(Environment.GetEnvironmentVariable("Test__SqlServer__DefaultConnection")).LogTo(Console.WriteLine,LogLevel.Information).EnableSensitiveDataLogging();protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){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"));}}publicabstractclassProduct{publicintId{get;privateset;}publicrequiredstringName{get;set;}}publicclassProduct1:Product{publicPrice?Price{get;set;}}publicclassProduct2:Product{publicPrice?Price{get;set;}}publicsealedclassPrice{publicrequiredstringAmount{get;init;}}
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
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)
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.
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
Activity
[-]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[/+]roji commentedon Dec 13, 2025
Confirmed, see exception below. @AndriySvyryd is this scenario supposed to be currently supported?
PoKNicolas commentedon Jan 13, 2026
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.
AndriySvyryd commentedon Jan 13, 2026
It will likely be fixed for EF Core 11, releasing in November, unless we get more reports
[-]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[/+]Fix NRE when TPH entities share nullable complex properties mapped to…
Fix: skip TryPropagate when containing complex property is null
Fix NullReferenceException when accessing null complex properties in …