-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Open
Description
Bug description
It is related to #37162
The issue still persist in the case where table A has an optional reference to table B, and table B has a complex property (with sqlite). Table B's complex property is null if all its values are null
Note, that if A's reference to table B is non-optional, it is parsed correctly.
Your code
using Microsoft.EntityFrameworkCore;
await using var context = new BugDbContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.EntityADbSet.Add(new EntityA
{
EntityB = new EntityB()
{
EntityBComplex = new EntityBComplex()
}
});
await context.SaveChangesAsync();
context.ChangeTracker.Clear();
var entityA = await context.EntityADbSet.Include(x => x.EntityB).SingleAsync();
Console.WriteLine($"entityA.EntityB.EntityBComplex is null: {entityA.EntityB.EntityBComplex is null} (expected: False)"); // true
public class BugDbContext : DbContext
{
public DbSet<EntityA> EntityADbSet { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlite("Data Source=bug.db");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EntityA>().HasOne(e => e.EntityB).WithMany().HasForeignKey(e => e.EntityBId);
modelBuilder.Entity<EntityB>().ComplexProperty(e => e.EntityBComplex).IsRequired();
}
}
public class EntityA
{
public int Id { get; set; }
public EntityB? EntityB { get; set; } // <== removing nullability gives expected behavior
public int? EntityBId { get; set; } // <== removing nullability gives expected behavior
}
public class EntityB
{
public int Id { get; set; }
public EntityBComplex EntityBComplex { get; set; }
}
public class EntityBComplex
{
public string? Value { get; set; }
}Stack traces
System.NullReferenceException: Object reference not set to an instance of an object.
Verbose output
entityA.EntityB.EntityBComplex is null: True (expected: False)
EF Core version
10.0.1
Database provider
Microsoft.EntityFrameworkCore.Sqlite
Target framework
.NET 10
Operating system
Windows 11
IDE
Rider 2025.3.0.3