Skip to content

Commit a55150b

Browse files
Copilotrenemadsen
andcommitted
Fix: Remove ClrType check - always convert to MemoryStream for JSON
The ClrType check was preventing conversion for complex JSON types because MySqlJsonTypeMapping<string> has ClrType = typeof(string) even when used for complex JSON properties. Remove the check to always convert string to MemoryStream for all JSON type mappings. This fixes ComplexJsonStructuralEqualityMySqlTest.Two_associates which was failing with "No coercion operator is defined between types 'System.String' and 'System.IO.MemoryStream'". Co-authored-by: renemadsen <76994+renemadsen@users.noreply.github.com>
1 parent e2285a2 commit a55150b

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,13 @@ 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-
/// 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).
147+
/// We always convert string to MemoryStream since JSON type mappings are used for complex types.
150148
/// </summary>
151149
public override Expression CustomizeDataReaderExpression(Expression expression)
152150
{
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))
151+
// Convert string to MemoryStream for JSON types.
152+
// EF Core uses MemoryStream for complex JSON properties.
153+
if (expression.Type == typeof(string))
156154
{
157155
// Validate that reflection lookups succeeded
158156
if (_utf8Property == null || _getBytesMethod == null || _memoryStreamCtor == null)
@@ -163,7 +161,6 @@ public override Expression CustomizeDataReaderExpression(Expression expression)
163161
}
164162

165163
// Convert string to MemoryStream: new MemoryStream(Encoding.UTF8.GetBytes(stringValue))
166-
// This is needed for complex JSON types where EF Core expects a stream
167164
return Expression.New(
168165
_memoryStreamCtor,
169166
Expression.Call(

0 commit comments

Comments
 (0)