Repository: dotnet/efcore
File: test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs (C#)

Code before:
    {
        var contextFactory = await InitializeAsync<Context37634>(
            seed: context =>
            {
                context.Add(
                    new Context37634.EntityType
                    {
                        Prop = new Context37634.OptionalComplexProperty
                        {
                            OptionalValue = true
                        }
                    });
                return context.SaveChangesAsync();
            });

        await using var context = contextFactory.CreateContext();

        var entities = await context.Set<Context37634.EntityType>().ToArrayAsync();

        Assert.Single(entities);
        var entity = entities[0];
        Assert.NotNull(entity.Prop);
        Assert.True(entity.Prop.OptionalValue);
    }

    private class Context37634(DbContextOptions options) : DbContext(options)
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var entity = modelBuilder.Entity<EntityType>();
            entity.Property(p => p.Id);
            entity.HasKey(p => p.Id);

            var compl = entity.ComplexProperty(p => p.Prop);
            compl.Property(p => p.OptionalValue);
            compl.HasDiscriminator();

            // Shadow property added via convention (e.g., audit field)
            entity.Property<string>("CreatedBy").IsRequired(false);
        }

        public class EntityType
        {
            public Guid Id { get; set; }
            public OptionalComplexProperty? Prop { get; set; }
        }

        public class OptionalComplexProperty
        {
            public bool? OptionalValue { get; set; }
        }

Review comment (suggestion):
The class name "Context37634" references issue #37634, but this PR is fixing issue #37337. The class should be renamed to "Context37337" to correctly reference the issue being addressed.

Code after:
    {
        var contextFactory = await InitializeAsync<Context37337>(
            seed: context =>
            {
                context.Add(
                    new Context37337.EntityType
                    {
                        Prop = new Context37337.OptionalComplexProperty
                        {
                            OptionalValue = true
                        }
                    });
                return context.SaveChangesAsync();
            });

        await using var context = contextFactory.CreateContext();

        var entities = await context.Set<Context37337.EntityType>().ToArrayAsync();

        Assert.Single(entities);
        var entity = entities[0];
        Assert.NotNull(entity.Prop);
        Assert.True(entity.Prop.OptionalValue);
    }

    private class Context37337(DbContextOptions options) : DbContext(options)
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var entity = modelBuilder.Entity<EntityType>();
            entity.Property(p => p.Id);
            entity.HasKey(p => p.Id);

            var compl = entity.ComplexProperty(p => p.Prop);
            compl.Property(p => p.OptionalValue);
            compl.HasDiscriminator();

            // Shadow property added via convention (e.g., audit field)
            entity.Property<string>("CreatedBy").IsRequired(false);
        }

        public class EntityType
        {
            public Guid Id { get; set; }
            public OptionalComplexProperty? Prop { get; set; }
        }

        public class OptionalComplexProperty
        {
            public bool? OptionalValue { get; set; }
        }