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

Code before:
                    new Context34706.Blog
                    {
                        ComplexThing = new Context34706.ComplexThing
                        {
                            Prop1 = 1,
                            Prop2 = 2
                        }
                    });
                await context.SaveChangesAsync();

                string Q(string name) => sqlGenerationHelper.DelimitIdentifier(name);
            });

        await using var context = contextFactory.CreateContext();

        var entity = await context.Set<Context34706.Blog>().SingleAsync();

        Assert.NotNull(entity.ComplexThing);
        Assert.Equal(1, entity.ComplexThing.Prop1);
        Assert.Equal(2, entity.ComplexThing.Prop2);
    }

    private class Context34706(DbContextOptions options) : DbContext(options)
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
            => modelBuilder.Entity<Blog>().ComplexProperty(b => b.ComplexThing);

        public class Blog
        {
            public int Id { get; set; }
            public required ComplexThing ComplexThing { get; set; }
        }

        public class ComplexThing
        {
            public int Prop1 { get; set; }
            public int Prop2 { get; set; }
        }
    }

    #endregion 34706

    protected TestSqlLoggerFactory TestSqlLoggerFactory
        => (TestSqlLoggerFactory)ListLoggerFactory;

    protected void AssertSql(params string[] expected)
        => TestSqlLoggerFactory.AssertBaseline(expected);
}


Review comment (refactor):
The ComplexProperty configuration is specified twice: once in the onModelCreating parameter passed to InitializeAsync (line 127) and once in the Context34706's OnModelCreating method (line 159). While EF Core will handle duplicate configuration calls gracefully, this is redundant. Consider removing the ComplexProperty call from the context's OnModelCreating method since it's already configured via the onModelCreating parameter, or move all configuration to the context's OnModelCreating and remove it from the onModelCreating parameter.
```suggestion
            => base.OnModelCreating(modelBuilder);
```

Code after:
                    new Context34706.Blog
                    {
                        ComplexThing = new Context34706.ComplexThing
                        {
                            Prop1 = 1,
                            Prop2 = 2
                        }
                    });
                await context.SaveChangesAsync();

                string Q(string name) => sqlGenerationHelper.DelimitIdentifier(name);
            });

        await using var context = contextFactory.CreateContext();

        var entity = await context.Set<Context34706.Blog>().SingleAsync();

        Assert.NotNull(entity.ComplexThing);
        Assert.Equal(1, entity.ComplexThing.Prop1);
        Assert.Equal(2, entity.ComplexThing.Prop2);
    }

    private class Context34706(DbContextOptions options) : DbContext(options)
    {
        public class Blog
        {
            public int Id { get; set; }
            public required ComplexThing ComplexThing { get; set; }
        }

        public class ComplexThing
        {
            public int Prop1 { get; set; }
            public int Prop2 { get; set; }
        }
    }

    #endregion 34706

    protected TestSqlLoggerFactory TestSqlLoggerFactory
        => (TestSqlLoggerFactory)ListLoggerFactory;

    protected void AssertSql(params string[] expected)
        => TestSqlLoggerFactory.AssertBaseline(expected);
}