You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cast to the closed ValueConverter<,> in the precompiled shaper - #39236
The relational shaper cast the type mapping's converter to the design-time converter class when materializing a property that requires a conversion. When the model is a compiled model, the converter is reconstructed as the base ValueConverter<TModel, TProvider> (the subclass is lost), so the generated cast to the subclass threw InvalidCastException at run time — under the JIT and in native AOT alike.
Cast to the closed ValueConverter<,> the converter implements (the typedConverterType already computed at each site) instead. This is what the compiled model reconstructs, and it remains valid for run-time models, where the converter instance is assignable to the closed generic.
I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
The code builds and tests pass locally (also verified by our automated build checks)
Commit messages follow the format described in the template
Tests were added
No breaking changes
Tests
Added AdHocPrecompiledQueryRelationalTestBase.Projecting_entity_with_value_converter_subclass_does_not_cast_to_the_subclass. It uses a ValueConverter<TModel, TProvider> subclass with distinct model and provider types (Rating → int) and asserts that the generated interceptor no longer casts the type mapping's converter to the design-time subclass.
Verification against the SQLite provider:
Fails on main — the generated interceptor contains ((...PrecompiledContext39174.RatingConverter)(((IReadOnlyProperty)ratingProperty).GetTypeMapping().Converter)).ConvertFromProviderTyped(...), which throws InvalidCastException when the model is a compiled model.
- The relational shaper cast the type mapping's converter to the design-time
converter class when materializing a property that requires a conversion.
- A compiled model reconstructs the converter as the base
ValueConverter<TModel, TProvider> (the subclass is lost), so the generated
cast to the subclass threw InvalidCastException at run time, under the JIT
and in native AOT alike.
- Cast to the closed ValueConverter<,> the converter implements
(typedConverterType) instead, which is what the compiled model
reconstructs and which remains valid for run-time models.
Fixesdotnet#39174
AdHocPrecompiledQuerySqlServerTest.Check_all_tests_overridden fails if a base
test is not overridden, so add the SqlServer override with its SQL baseline.
The new test covers a regular column, but this is a separate converter cast for a nullable property inside a JSON-mapped entity whose converter handles nulls. A subclass cast emitted here would still fail against the base converter reconstructed by a compiled model, while the added test would pass. Please add a precompiled JSON-property test with a ValueConverter<,> subclass using convertsNulls: true, a JSON null token, and an interceptor-code assertion that the subclass cast is absent.
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
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.
Fixes #39174
The relational shaper cast the type mapping's converter to the design-time converter class when materializing a property that requires a conversion. When the model is a compiled model, the converter is reconstructed as the base
ValueConverter<TModel, TProvider>(the subclass is lost), so the generated cast to the subclass threwInvalidCastExceptionat run time — under the JIT and in native AOT alike.Cast to the closed
ValueConverter<,>the converter implements (thetypedConverterTypealready computed at each site) instead. This is what the compiled model reconstructs, and it remains valid for run-time models, where the converter instance is assignable to the closed generic.Checklist
Tests
Added
AdHocPrecompiledQueryRelationalTestBase.Projecting_entity_with_value_converter_subclass_does_not_cast_to_the_subclass. It uses aValueConverter<TModel, TProvider>subclass with distinct model and provider types (Rating→int) and asserts that the generated interceptor no longer casts the type mapping's converter to the design-time subclass.Verification against the SQLite provider:
main— the generated interceptor contains((...PrecompiledContext39174.RatingConverter)(((IReadOnlyProperty)ratingProperty).GetTypeMapping().Converter)).ConvertFromProviderTyped(...), which throwsInvalidCastExceptionwhen the model is a compiled model.AdHocPrecompiledQuerySqliteTest: 28/28.PrecompiledQuerySqliteTest: 124 passed, 4 skipped.CompiledModelSqliteTest: 16/16.