Skip to content

Commit e2285a2

Browse files
Copilotrenemadsen
andcommitted
Fix: Only convert to MemoryStream for non-string CLR types
The previous fix converted ALL JSON strings to MemoryStream, breaking regular JSON columns mapped to string. Now only converts to MemoryStream when ClrType != typeof(string), i.e., for complex JSON types only. Fixes the regression where BuiltInDataTypesMySqlTest.Can_insert_and_read_back_all_mapped_data_types_set_to_null_in_batch was failing with "No coercion operator is defined between types 'System.IO.MemoryStream' and 'System.String'". Co-authored-by: renemadsen <76994+renemadsen@users.noreply.github.com>
1 parent db4480e commit e2285a2

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/EFCore.MySql/Storage/Internal/MySqlJsonTypeMapping.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,15 @@ public override MethodInfo GetDataReaderMethod()
144144
/// <summary>
145145
/// Customizes the data reader expression for JSON types.
146146
/// MySQL stores JSON as strings, but EF Core expects MemoryStream for complex JSON types.
147-
/// This method creates an expression that converts the string to a MemoryStream containing UTF-8 encoded bytes.
147+
/// For regular JSON columns mapped to string CLR type, no conversion is needed.
148+
/// This method creates an expression that converts the string to a MemoryStream containing UTF-8 encoded bytes
149+
/// only when the CLR type is not string (i.e., for complex types).
148150
/// </summary>
149151
public override Expression CustomizeDataReaderExpression(Expression expression)
150152
{
151-
// For JSON complex types, EF Core expects a MemoryStream containing JSON data.
152-
// MySQL returns JSON as strings, so we need to convert: string -> byte[] -> MemoryStream
153-
154-
if (expression.Type == typeof(string))
153+
// Only convert to MemoryStream for non-string CLR types (complex JSON types).
154+
// For string CLR types (regular JSON columns), return the string as-is.
155+
if (expression.Type == typeof(string) && ClrType != typeof(string))
155156
{
156157
// Validate that reflection lookups succeeded
157158
if (_utf8Property == null || _getBytesMethod == null || _memoryStreamCtor == null)
@@ -162,6 +163,7 @@ public override Expression CustomizeDataReaderExpression(Expression expression)
162163
}
163164

164165
// Convert string to MemoryStream: new MemoryStream(Encoding.UTF8.GetBytes(stringValue))
166+
// This is needed for complex JSON types where EF Core expects a stream
165167
return Expression.New(
166168
_memoryStreamCtor,
167169
Expression.Call(

0 commit comments

Comments
 (0)