Use ConstructorExpression in CSharpRuntimeAnnotationCodeGenerator#38409
Draft
Copilot wants to merge 3 commits into
Draft
Use ConstructorExpression in CSharpRuntimeAnnotationCodeGenerator#38409Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
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>
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))))); |
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.unsafeAccessorsset instead of creating leaf-local sets, so generated unsafe accessors are correctly propagated.ValueConverter/ValueConverter<TModel,TProvider>ConstructorExpressionWithoutMappingHints(base virtual + generic override).ConstructorExpressionstill handlesconvertsNullsvia the 4-arg constructor when needed.ConstructorExpressionWithoutMappingHintsreuses the same constructor shape but emitsdefault(ConverterMappingHints)instead of concrete mapping-hint values.ValueConverter.MappingHintsCtor(latent bug)ConverterMappingHintsconstructor and returnednull. 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.