diff --git a/src/EFCore/ChangeTracking/Internal/SnapshotFactoryFactory.cs b/src/EFCore/ChangeTracking/Internal/SnapshotFactoryFactory.cs index b4da0cb54ad..a4e6a57cd36 100644 --- a/src/EFCore/ChangeTracking/Internal/SnapshotFactoryFactory.cs +++ b/src/EFCore/ChangeTracking/Internal/SnapshotFactoryFactory.cs @@ -129,7 +129,9 @@ protected virtual Expression CreateSnapshotExpression( case IProperty property: // Shadow property materialization on complex types is not currently supported (see #35613). - if (propertyBase.DeclaringType is IComplexType && property.IsShadowProperty()) + if (propertyBase.DeclaringType is IComplexType + && property.IsShadowProperty() + && (parameter == null || !parameter.Type.IsAssignableTo(typeof(IInternalEntry)) { arguments[i] = propertyBase.ClrType.GetDefaultValueConstant(); types[i] = propertyBase.ClrType; diff --git a/test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs index b5d5739c8e3..7db32f29df9 100644 --- a/test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs @@ -379,6 +379,62 @@ public class OptionalComplexProperty #endregion Issue37337 + #region Issue37914 + + [ConditionalFact] + public virtual async Task Update_entity_with_nullable_complex_type_and_discriminator() + { + var contextFactory = await InitializeAsync( + seed: context => + { + context.Add( + new Context37914.EntityType + { + Prop = new Context37914.OptionalComplexProperty + { + OptionalValue = true + } + }); + return context.SaveChangesAsync(); + }); + + await using var context = contextFactory.CreateContext(); + + var entity = await context.Set().SingleAsync(); + Assert.NotNull(entity.Prop); + Assert.True(entity.Prop.OptionalValue); + + context.Update(entity); + await context.SaveChangesAsync(); + } + + private class Context37914(DbContextOptions options) : DbContext(options) + { + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + var entity = modelBuilder.Entity(); + entity.Property(p => p.Id); + entity.HasKey(p => p.Id); + + var compl = entity.ComplexProperty(p => p.Prop); + compl.Property(p => p.OptionalValue); + compl.HasDiscriminator(); + } + + public class EntityType + { + public Guid Id { get; set; } + public OptionalComplexProperty? Prop { get; set; } + } + + public class OptionalComplexProperty + { + public bool? OptionalValue { get; set; } + } + } + + #endregion Issue37914 + protected override string NonSharedStoreName => "AdHocComplexTypeQueryTest"; }