@@ -513,6 +513,13 @@ protected override Expression VisitJsonScalar(JsonScalarExpression jsonScalarExp
513513 castStoreType = GetCastStoreType ( jsonScalarExpression . TypeMapping ) ;
514514 // }
515515
516+ // MariaDB does not support CAST(... AS json), so skip the CAST when targeting json type with JsonDataTypeEmulation enabled.
517+ // This prevents SQL syntax errors like "near 'json) IS NULL'" on MariaDB.
518+ if ( castStoreType == "json" && _options . ServerVersion . Supports . JsonDataTypeEmulation )
519+ {
520+ castStoreType = null ;
521+ }
522+
516523 if ( castStoreType is not null )
517524 {
518525 Sql . Append ( "CAST(" ) ;
@@ -718,9 +725,15 @@ private SqlUnaryExpression VisitConvert(SqlUnaryExpression sqlUnaryExpression)
718725 operandUnary . OperatorType == ExpressionType . Convert &&
719726 castMapping . Equals ( GetCastStoreType ( operandUnary . TypeMapping ) , StringComparison . OrdinalIgnoreCase ) ;
720727
721- if ( castMapping == "json" && ! _options . ServerVersion . Supports . JsonDataTypeEmulation ||
722- ! castMapping . Equals ( sqlUnaryExpression . Operand . TypeMapping . StoreType , StringComparison . OrdinalIgnoreCase ) &&
723- ! sameInnerCastStoreType )
728+ // MariaDB does not support CAST(... AS json) syntax, so we skip the CAST when JsonDataTypeEmulation is enabled (MariaDB).
729+ // For MySQL 8+, which has native JSON type support, we generate the CAST when needed.
730+ // This prevents SQL syntax errors like "near 'json)) IS NULL'" on MariaDB.
731+ var skipCastForMariaDbJson = castMapping == "json" && _options . ServerVersion . Supports . JsonDataTypeEmulation ;
732+
733+ if ( ! skipCastForMariaDbJson &&
734+ ( castMapping == "json" && ! _options . ServerVersion . Supports . JsonDataTypeEmulation ||
735+ ! castMapping . Equals ( sqlUnaryExpression . Operand . TypeMapping . StoreType , StringComparison . OrdinalIgnoreCase ) &&
736+ ! sameInnerCastStoreType ) )
724737 {
725738 var useDecimalToDoubleWorkaround = false ;
726739
0 commit comments