Skip to content

Commit 2c11e89

Browse files
Copilotrenemadsen
andcommitted
Use fallback type mapping when FindMapping returns null
Instead of skipping the JSON conversion when FindMapping returns null, use the parameter's existing TypeMapping as a fallback. This prevents NullReferenceException while maintaining correct query behavior. Co-authored-by: renemadsen <[email protected]>
1 parent a38d24e commit 2c11e89

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/EFCore.MySql/Query/ExpressionVisitors/Internal/MySqlJsonParameterExpressionVisitor.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ protected virtual SqlExpression VisitParameter(SqlParameterExpression sqlParamet
3737

3838
// MySQL has a real JSON datatype, and string parameters need to be converted to it.
3939
// MariaDB defines the JSON datatype just as a synonym for LONGTEXT.
40-
if (typeMapping is not null && !_options.ServerVersion.Supports.JsonDataTypeEmulation)
40+
if (!_options.ServerVersion.Supports.JsonDataTypeEmulation)
4141
{
42+
// Use the found type mapping, or fall back to the parameter's existing type mapping if FindMapping returns null
43+
var targetTypeMapping = typeMapping ?? sqlParameterExpression.TypeMapping;
44+
4245
return _sqlExpressionFactory.Convert(
4346
sqlParameterExpression,
44-
typeMapping.ClrType, // will be typeof(string) when `sqlParameterExpression.Type`
45-
typeMapping); // is typeof(MySqlJsonString)
47+
targetTypeMapping.ClrType, // will be typeof(string) when `sqlParameterExpression.Type`
48+
targetTypeMapping); // is typeof(MySqlJsonString)
4649
}
4750
}
4851

0 commit comments

Comments
 (0)