Skip to content

Commit 4448ed5

Browse files
Copilotrenemadsen
andcommitted
Fix: Match PostgreSQL implementation - use dbType: null
Changed MySqlStructuralJsonTypeMapping to match PostgreSQL's Npgsql implementation: 1. Use `dbType: null` instead of `DbType.String` in base constructor 2. Use inline Expression.New() for CustomizeDataReaderExpression like Npgsql 3. Match PostgreSQL's static field pattern for reflection lookups PostgreSQL successfully uses JsonTypePlaceholder as ClrType with these exact patterns. The dbType: null parameter is critical for proper SQL column projection. Co-authored-by: renemadsen <76994+renemadsen@users.noreply.github.com>
1 parent c9b7cfc commit 4448ed5

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,26 @@ namespace Pomelo.EntityFrameworkCore.MySql.Storage.Internal
1515
/// <summary>
1616
/// Type mapping for complex JSON types (types mapped with .ToJson()).
1717
/// This mapping handles the conversion from MySQL's string-based JSON to MemoryStream expected by EF Core.
18+
/// Similar to Npgsql's NpgsqlStructuralJsonTypeMapping.
1819
/// </summary>
1920
public class MySqlStructuralJsonTypeMapping : JsonTypeMapping
2021
{
21-
private static readonly MethodInfo _createUtf8StreamMethod
22-
= typeof(MySqlStructuralJsonTypeMapping).GetMethod(nameof(CreateUtf8Stream), new[] { typeof(string) });
23-
2422
private static readonly MethodInfo _getStringMethod
2523
= typeof(DbDataReader).GetRuntimeMethod(nameof(DbDataReader.GetString), new[] { typeof(int) });
2624

25+
private static readonly PropertyInfo _utf8Property
26+
= typeof(Encoding).GetProperty(nameof(Encoding.UTF8));
27+
28+
private static readonly MethodInfo _getBytesMethod
29+
= typeof(Encoding).GetMethod(nameof(Encoding.GetBytes), new[] { typeof(string) });
30+
31+
private static readonly ConstructorInfo _memoryStreamConstructor
32+
= typeof(MemoryStream).GetConstructor(new[] { typeof(byte[]) });
33+
2734
public static MySqlStructuralJsonTypeMapping Default { get; } = new("json");
2835

2936
public MySqlStructuralJsonTypeMapping(string storeType)
30-
: base(storeType, typeof(JsonTypePlaceholder), System.Data.DbType.String)
37+
: base(storeType, typeof(JsonTypePlaceholder), dbType: null)
3138
{
3239
Console.WriteLine($"[DEBUG] MySqlStructuralJsonTypeMapping created - StoreType: {storeType}, ClrType: JsonTypePlaceholder");
3340
}
@@ -47,19 +54,19 @@ public override MethodInfo GetDataReaderMethod()
4754
return _getStringMethod;
4855
}
4956

50-
/// <summary>
51-
/// Converts the string read from MySQL to a MemoryStream for EF Core's JSON processing.
52-
/// </summary>
53-
public static MemoryStream CreateUtf8Stream(string json)
54-
=> new MemoryStream(Encoding.UTF8.GetBytes(json ?? string.Empty));
55-
5657
/// <summary>
5758
/// Customizes the data reader expression to convert string to MemoryStream.
59+
/// Creates: new MemoryStream(Encoding.UTF8.GetBytes(stringValue))
5860
/// </summary>
5961
public override Expression CustomizeDataReaderExpression(Expression expression)
6062
{
6163
Console.WriteLine("[DEBUG] MySqlStructuralJsonTypeMapping.CustomizeDataReaderExpression() called - converting string to MemoryStream");
62-
return Expression.Call(_createUtf8StreamMethod, expression);
64+
return Expression.New(
65+
_memoryStreamConstructor,
66+
Expression.Call(
67+
Expression.Property(null, _utf8Property),
68+
_getBytesMethod,
69+
expression));
6370
}
6471

6572
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)

0 commit comments

Comments
 (0)