Repository navigation
Description
Bug description
When two or more derived types in a TPH hierarchy each declare an optional (nullable) complex property that maps to the same column names, setting the property to null and calling SaveChanges() does not write anything. The change tracker marks the entity as Modified, but the generated UPDATE leaves out the complex property's columns, so the old values stay in the database.
If you change to .NET 10 and EF Core 10 in the repro code you see that everything works fine so the issue got introduced in .NET 11 / EF Core 11. This also happens using the Npgsql and SQL Server provider so it's not SQLite in itself.
These cases are not affected:
- an optional complex property on a single derived type in a TPH hierarchy (The
Singleclass in the repro code) - an optional complex property on an entity with no inheritance (The
Plainclass in the repro code)
Your code
// repro.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net11.0</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="11.0.0" />
</ItemGroup>
</Project>
// repro.cs
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using (var ctx = new Ctx())
{
ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();
ctx.Add(new Single { Id = 1, Value = new Num(1m, 2m, 3) });
ctx.Add(new A { Id = 2, Value = new Num(1m, 2m, 3) });
ctx.Add(new B { Id = 3, Value = new Num(1m, 2m, 3) });
ctx.Add(new Plain { Id = 4, Value = new Num(1m, 2m, 3) });
ctx.SaveChanges();
}
using (var ctx = new Ctx())
{
// THESE HERE SHOULD ALL GENERATE `UPDATE ... SET value = NULL, value_err = NULL, value_prec = NULL`
foreach (var e in ctx.Set<Single>()) e.Value = null;
foreach (var e in ctx.Set<A>()) e.Value = null;
foreach (var e in ctx.Set<B>()) e.Value = null;
foreach (var e in ctx.Set<Plain>()) e.Value = null;
ctx.SaveChanges();
}
using (var ctx = new Ctx())
{
Console.WriteLine("--- RESULT (expected: all four Value == null) ---");
Console.WriteLine($"Single (TPH, 1 derived) : {ctx.Set<Single>().Single().Value?.ToString() ?? "null"}");
Console.WriteLine($"A (TPH, shared cols): {ctx.Set<A>().Single().Value?.ToString() ?? "null"}");
Console.WriteLine($"B (TPH, shared cols): {ctx.Set<B>().Single().Value?.ToString() ?? "null"}");
Console.WriteLine($"Plain (no inheritance) : {ctx.Set<Plain>().Single().Value?.ToString() ?? "null"}");
}
public sealed record Num(decimal Value, decimal Error, short Precision);
public abstract class Base1 { public int Id { get; set; } }
public sealed class Single : Base1 { public Num? Value { get; set; } }
public abstract class Base2 { public int Id { get; set; } }
public sealed class A : Base2 { public Num? Value { get; set; } }
public sealed class B : Base2 { public Num? Value { get; set; } }
public sealed class Plain { public int Id { get; set; } public Num? Value { get; set; } }
public sealed class Ctx : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder o) =>
o.UseSqlite("Data Source=repro.db");
protected override void OnModelCreating(ModelBuilder m)
{
m.Entity<Base1>().HasDiscriminator<int>("type").HasValue<Single>(0);
m.Entity<Base2>().HasDiscriminator<int>("type").HasValue<A>(0).HasValue<B>(1);
m.Entity<Single>().ComplexProperty(p => p.Value, Map);
m.Entity<A>().ComplexProperty(p => p.Value, Map);
m.Entity<B>().ComplexProperty(p => p.Value, Map);
m.Entity<Plain>().ComplexProperty(p => p.Value, Map);
static void Map(Microsoft.EntityFrameworkCore.Metadata.Builders.ComplexPropertyBuilder<Num> c)
{
c.Property(p => p.Value).HasColumnName("value");
c.Property(p => p.Error).HasColumnName("value_err");
c.Property(p => p.Precision).HasColumnName("value_prec");
}
}
}Verbose output
// .NET 10
--- RESULT (expected: all four Value == null) ---
Single (TPH, 1 derived) : null
A (TPH, shared cols): null
B (TPH, shared cols): null
Plain (no inheritance) : null
// .NET 11 (NOTICE ROW 2/3)
--- RESULT (expected: all four Value == null) ---
Single (TPH, 1 derived) : null
A (TPH, shared cols): Num { Value = 1.0, Error = 2.0, Precision = 3 }
B (TPH, shared cols): Num { Value = 1.0, Error = 2.0, Precision = 3 }
Plain (no inheritance) : null
EF Core version
11.0.0
Database provider
Microsoft.EntityFrameworkCore.Sqlite
Target framework
.NET 11
Operating system
macOS 27.2 or Windows 11
IDE
Visual Studio Code 1.145.2
Reacted by Copilot
Activity
[release/11.0] Persist null complex properties on TPH siblings with s…