Skip to content

Commit c9b7cfc

Browse files
Copilotrenemadsen
andcommitted
Fix: Check JsonTypePlaceholder BEFORE store type lookups
Moved the JsonTypePlaceholder check to the very beginning of FindRawMapping, before any store type lookups. This matches SQL Server's implementation and ensures the structural JSON type mapping is returned correctly. The previous placement after store type lookups was causing EF Core to find regular JSON mappings first, leading to SQL generation issues with empty columns in SELECT statements. Co-authored-by: renemadsen <76994+renemadsen@users.noreply.github.com>
1 parent 85a75a0 commit c9b7cfc

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,15 @@ private RelationalTypeMapping FindRawMapping(RelationalTypeMappingInfo mappingIn
312312
var storeTypeName = mappingInfo.StoreTypeName;
313313
var storeTypeNameBase = mappingInfo.StoreTypeNameBase;
314314

315+
// Special case for JSON columns: EF Core passes JsonTypePlaceholder as the CLR type
316+
// when creating JSON columns for complex types/collections. Return our structural JSON mapping.
317+
// This MUST be checked first, before any store type lookups, similar to SQL Server's implementation.
318+
if (clrType?.Name == "JsonTypePlaceholder")
319+
{
320+
Console.WriteLine($"[DEBUG] MySqlTypeMappingSource: Detected JsonTypePlaceholder - returning MySqlStructuralJsonTypeMapping");
321+
return _jsonStructural;
322+
}
323+
315324
if (storeTypeName != null)
316325
{
317326
// First look for the fully qualified store type name.
@@ -323,14 +332,6 @@ private RelationalTypeMapping FindRawMapping(RelationalTypeMappingInfo mappingIn
323332
// If a CLR type was provided, look for a mapping between the store and CLR types. If none is found,
324333
// fail immediately.
325334

326-
// Special case for JSON columns: EF Core passes JsonTypePlaceholder as the CLR type
327-
// when creating JSON columns for complex types/collections. Return our structural JSON mapping.
328-
if (clrType?.Name == "JsonTypePlaceholder" && storeTypeName.Equals("json", StringComparison.OrdinalIgnoreCase))
329-
{
330-
Console.WriteLine($"[DEBUG] MySqlTypeMappingSource: Detected JsonTypePlaceholder for JSON column - returning MySqlStructuralJsonTypeMapping");
331-
return _jsonStructural;
332-
}
333-
334335
return clrType == null
335336
? mappings[0]
336337
: mappings.FirstOrDefault(m => m.ClrType == clrType);

0 commit comments

Comments
 (0)