Use ConstructorExpression in CSharpRuntimeAnnotationCodeGenerator#38409
Merged
AndriySvyryd merged 9 commits intoJun 17, 2026
Merged
Conversation
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
There was a problem hiding this comment.
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
CSharpRuntimeAnnotationCodeGeneratorto render converter/reader-writer construction viaICSharpHelper.Expression(...ConstructorExpression...)(while keeping the existing recursive special-cases whereConstructorExpressioncontains non-renderable constants). - Fix
ValueConverter<TModel,TProvider>.ConstructorExpressionto correctly representConvertsNullsby using the 4-arg constructor when needed, and to emit mapping-hints via the correctConverterMappingHints(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). |
AndriySvyryd
approved these changes
Jun 11, 2026
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…from ValueConverter constructors Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…pingHints Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…onverter to single-line Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
cincuranet
approved these changes
Jun 16, 2026
Contributor
API review baseline changes for
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The compiled-model generator hand-rolled C# for
ValueConverterandJsonValueReaderWriterinstances via reflection-based string building, duplicating logic already available on each type's constructor expression metadata. This change has the generator render those expressions throughICSharpHelper.Expression(...), with reviewer follow-up fixes applied.Changes
CSharpRuntimeAnnotationCodeGeneratorcodeHelper.Expression(converter.ConstructorExpressionWithoutMappingHints, ...)so compiled models don't redundantly persistConverterMappingHintswhen type mapping is already explicit.codeHelper.Expression(jsonValueReaderWriter.ConstructorExpression, ...).CollectionToJsonStringConverter,IJsonConvertedValueReaderWriter, andICompositeJsonValueReaderWriter: theirConstructorExpressionembedsExpression.Constant(converterInstance), which cannot be rendered to source.missingUnsafeAccessorsis created only in the scope where constructor expressions are rendered and asserted empty there (post-validation only).ValueConverter/ValueConverter<TModel,TProvider>ConstructorExpressionWithoutMappingHints(base virtual + generic override).ConstructorExpressionstill handlesconvertsNullsvia the 4-arg constructor when needed.ConstructorExpressionWithoutMappingHintsuses dedicated 2-arg(convertTo, convertFrom)and 3-arg(convertTo, convertFrom, convertsNulls)constructors so noConverterMappingHintsargument is passed at all — not evendefault(ConverterMappingHints).ValueConverter.MappingHintsCtor(latent bug)ConverterMappingHintsconstructor and returnednull. Corrected to the current 4-arg(int?, int?, int?, bool?)constructor.Regenerated baselines
src/EFCore/EFCore.baseline.jsonwas updated by runningEFCore.ApiBaseline.Testslocally.