Skip to content

Use ConstructorExpression in CSharpRuntimeAnnotationCodeGenerator#38409

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/use-constructor-expression-in-csharpruntimeannotat
Draft

Use ConstructorExpression in CSharpRuntimeAnnotationCodeGenerator#38409
Copilot wants to merge 3 commits into
mainfrom
copilot/use-constructor-expression-in-csharpruntimeannotat

Conversation

Copilot AI commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

The compiled-model generator hand-rolled C# for ValueConverter and JsonValueReaderWriter instances via reflection-based string building, duplicating logic already available on each type's constructor expression metadata. This change has the generator render those expressions through ICSharpHelper.Expression(...), with reviewer follow-up fixes applied.

Changes

  • CSharpRuntimeAnnotationCodeGenerator

    • Plain converters now emit codeHelper.Expression(converter.ConstructorExpressionWithoutMappingHints, ...) so compiled models don't redundantly persist ConverterMappingHints when type mapping is already explicit.
    • Leaf JSON reader/writers emit codeHelper.Expression(jsonValueReaderWriter.ConstructorExpression, ...).
    • Recursion is intentionally retained for CollectionToJsonStringConverter, IJsonConvertedValueReaderWriter, and ICompositeJsonValueReaderWriter: their ConstructorExpression embeds Expression.Constant(converterInstance), which cannot be rendered to source.
    • Recursive converter/JSON generation now threads through the same unsafeAccessors set instead of creating leaf-local sets, so generated unsafe accessors are correctly propagated.
  • ValueConverter / ValueConverter<TModel,TProvider>

    • Added ConstructorExpressionWithoutMappingHints (base virtual + generic override).
    • ConstructorExpression still handles convertsNulls via the 4-arg constructor when needed.
    • ConstructorExpressionWithoutMappingHints reuses the same constructor shape but emits default(ConverterMappingHints) instead of concrete mapping-hint values.
  • ValueConverter.MappingHintsCtor (latent bug)

    • Previously resolved a non-existent 5-arg ConverterMappingHints constructor and returned null. Corrected to the current 4-arg (int?, int?, int?, bool?) constructor.
  • Regenerated baselines for compiled-model tests to match the new output (notably default(ConverterMappingHints) in generated converter construction where mapping hints are omitted from compiled-model serialization).

Note for reviewers

SqlServer and Cosmos baselines were not regenerated here — their test fixtures require a live SQL Server / Cosmos emulator during setup, before code generation runs. The generated output is provider-independent and was verified against InMemory and Sqlite, so these baselines can be regenerated in CI.

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Use ConstructorExpression in CSharpRuntimeAnnotationCodeGenerator Use ConstructorExpression in CSharpRuntimeAnnotationCodeGenerator Jun 11, 2026
Copilot AI requested a review from AndriySvyryd June 11, 2026 01:49
@AndriySvyryd AndriySvyryd requested a review from Copilot June 11, 2026 02:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates EF Core’s compiled-model code generation to reuse each ValueConverter/JsonValueReaderWriter instance’s ConstructorExpression instead of hand-rolled C# string construction. This consolidates logic, fixes a latent reflection bug in MappingHintsCtor, and makes generated compiled-model baselines more faithful by including mapping-hint values.

Changes:

  • Switch CSharpRuntimeAnnotationCodeGenerator to render converter/reader-writer construction via ICSharpHelper.Expression(...ConstructorExpression...) (while keeping the existing recursive special-cases where ConstructorExpression contains non-renderable constants).
  • Fix ValueConverter<TModel,TProvider>.ConstructorExpression to correctly represent ConvertsNulls by using the 4-arg constructor when needed, and to emit mapping-hints via the correct ConverterMappingHints(int?, int?, int?, bool?) constructor.
  • Regenerate InMemory/Sqlite compiled-model scaffolding baselines to reflect the new (single-line) emitted constructor expressions and mapping-hint arguments.

Reviewed changes

Copilot reviewed 18 out of 20 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/EFCore/Design/Internal/CSharpRuntimeAnnotationCodeGenerator.cs Use ConstructorExpression rendering for plain converters and leaf JSON reader/writers; retain recursion for converted/composite cases.
src/EFCore/Storage/ValueConversion/ValueConverter.cs Fix MappingHintsCtor reflection to target the existing 4-arg ConverterMappingHints constructor.
src/EFCore/Storage/ValueConversion/ValueConverter`.cs Update ConstructorExpression to include ConvertsNulls and emit mapping-hints via the corrected ctor.
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/ManyTypesEntityType.cs Baseline regen: converters now include mapping-hints args and single-line ctor emission.
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel/PrincipalBaseEntityType.cs Baseline regen for converter/JSON reader-writer construction output.
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel/OwnedTypeEntityType.cs Baseline regen for converter/JSON reader-writer construction output.
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel/OwnedType0EntityType.cs Baseline regen for converter/JSON reader-writer construction output.
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel/DependentBaseEntityType.cs Baseline regen for converter/JSON reader-writer construction output.
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/PrincipalBaseEntityType.cs Baseline regen for converter/JSON reader-writer construction output.
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/OwnedTypeEntityType.cs Baseline regen for converter/JSON reader-writer construction output.
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/OwnedType0EntityType.cs Baseline regen for converter/JSON reader-writer construction output.
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DependentBaseEntityType.cs Baseline regen for converter/JSON reader-writer construction output.
test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Self_referential_property/SelfReferentialEntityEntityType.cs Baseline regen: converter ctor now includes mapping-hints emission.
test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/RelationshipCycles/PrincipalBaseEntityType.cs Baseline regen for converter/JSON reader-writer construction output.
test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/ManyTypesEntityType.cs Baseline regen: converters now include mapping-hints args and single-line ctor emission.
test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_value_converter/MyEntityEntityType.cs Baseline regen for converter construction output.
test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/ComplexTypes/PrincipalDerivedEntityType.cs Baseline regen for converter/JSON reader-writer construction output (complex types).
test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/ComplexTypes/PrincipalBaseEntityType.cs Baseline regen for converter/JSON reader-writer construction output (complex types).

Comment thread src/EFCore/Design/Internal/CSharpRuntimeAnnotationCodeGenerator.cs Outdated
Comment thread src/EFCore/Design/Internal/CSharpRuntimeAnnotationCodeGenerator.cs Outdated
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 18 out of 20 changed files in this pull request and generated 1 comment.

Comment on lines 83 to +88
mappingInfo: new RelationalTypeMappingInfo(
size: 1),
converter: new ValueConverter<bool, string>(
string (bool v) => ((string)((v ? "B" : "A"))),
bool (string v) => !(string.IsNullOrEmpty(v)) && ((int)(v.ToUpperInvariant()[0])) == ((int)("B".ToUpperInvariant()[0]))),
converter: new ValueConverter<bool, string>(string (bool v) => ((string)((v ? "B" : "A"))), bool (string v) => !(string.IsNullOrEmpty(v)) && ((int)(v.ToUpperInvariant()[0])) == ((int)("B".ToUpperInvariant()[0])), new ConverterMappingHints(1, (int? )(null), (int? )(null), (bool? )(null))),
jsonValueReaderWriter: new JsonConvertedValueReaderWriter<bool, string>(
JsonStringReaderWriter.Instance,
new ValueConverter<bool, string>(
string (bool v) => ((string)((v ? "B" : "A"))),
bool (string v) => !(string.IsNullOrEmpty(v)) && ((int)(v.ToUpperInvariant()[0])) == ((int)("B".ToUpperInvariant()[0])))));
new ValueConverter<bool, string>(string (bool v) => ((string)((v ? "B" : "A"))), bool (string v) => !(string.IsNullOrEmpty(v)) && ((int)(v.ToUpperInvariant()[0])) == ((int)("B".ToUpperInvariant()[0])), new ConverterMappingHints(1, (int? )(null), (int? )(null), (bool? )(null)))));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use ConstructorExpression in CSharpRuntimeAnnotationCodeGenerator

3 participants