Skip to content

Commit f99b5dd

Browse files
Copilotrenemadsen
andcommitted
Fix: Use MemoryStream as ClrType for complex JSON types
Changed _jsonComplex to use MySqlJsonTypeMapping<MemoryStream> instead of byte[]. This makes the ClrType match what EF Core expects for complex JSON types, while still reading as string and converting in CustomizeDataReaderExpression. Updated the condition to check for MemoryStream type instead of non-string. This should resolve both the coercion error and SQL generation issues because: - ClrType = MemoryStream matches EF Core's expectations for complex JSON - Still reads as string from MySQL (GetDataReaderMethod returns GetString) - Converts string to MemoryStream only when ClrType == MemoryStream - Regular JSON columns use MySqlJsonTypeMapping<string> with no conversion Co-authored-by: renemadsen <76994+renemadsen@users.noreply.github.com>
1 parent c4aa7e0 commit f99b5dd

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ public override MethodInfo GetDataReaderMethod()
143143
/// <summary>
144144
/// Customizes the data reader expression for JSON types.
145145
/// MySQL stores JSON as strings, but EF Core expects MemoryStream for complex JSON types.
146-
/// We only convert for non-string CLR types (complex JSON types), not for regular JSON columns mapped to string.
146+
/// We only convert when ClrType is MemoryStream (complex JSON types), not for regular JSON columns mapped to string.
147147
/// </summary>
148148
public override Expression CustomizeDataReaderExpression(Expression expression)
149149
{
150-
// Only convert for complex JSON types (where ClrType is NOT string)
150+
// Only convert for complex JSON types (where ClrType is MemoryStream)
151151
// For regular JSON columns mapped to string, don't convert
152-
if (expression.Type == typeof(string) && ClrType != typeof(string))
152+
if (expression.Type == typeof(string) && ClrType == typeof(System.IO.MemoryStream))
153153
{
154154
// Validate that reflection lookups succeeded
155155
if (_utf8Property == null || _getBytesMethod == null || _memoryStreamCtor == null)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private readonly RelationalTypeMapping _binaryRowVersion6
9090
// JSON default mapping
9191
private MySqlJsonTypeMapping<string> _jsonDefaultString;
9292
// JSON mapping for complex types (JsonTypePlaceholder)
93-
private MySqlJsonTypeMapping<byte[]> _jsonComplex;
93+
private MySqlJsonTypeMapping<System.IO.MemoryStream> _jsonComplex;
9494

9595
// Scaffolding type mappings
9696
private readonly MySqlCodeGenerationMemberAccessTypeMapping _codeGenerationMemberAccess = MySqlCodeGenerationMemberAccessTypeMapping.Default;
@@ -137,7 +137,7 @@ private void Initialize()
137137
: null;
138138

139139
_jsonDefaultString = new MySqlJsonTypeMapping<string>("json", null, null, _options.NoBackslashEscapes, _options.ReplaceLineBreaksWithCharFunction);
140-
_jsonComplex = new MySqlJsonTypeMapping<byte[]>("json", null, null, _options.NoBackslashEscapes, _options.ReplaceLineBreaksWithCharFunction);
140+
_jsonComplex = new MySqlJsonTypeMapping<System.IO.MemoryStream>("json", null, null, _options.NoBackslashEscapes, _options.ReplaceLineBreaksWithCharFunction);
141141

142142
_storeTypeMappings
143143
= new Dictionary<string, RelationalTypeMapping[]>(StringComparer.OrdinalIgnoreCase)

0 commit comments

Comments
 (0)