EF Core 10 requires complex collections mapped with .ToJson() to have a provider-specific store type set. Tests were failing with:
The store type 'null' specified for JSON column 'Departments' is not supported by the current provider.
Both work without setting ContainerColumnType annotation because:
- SQLite: Default string mapping is "TEXT" (valid for JSON)
- SQL Server: Default string mapping is "nvarchar(max)" (valid for JSON)
- MySQL: Default string mapping is "longtext" (EF Core doesn't accept this for JSON)
- Missing annotation:
ContainerColumnTypenot set to "json" on complex types - Type mapping restriction:
MySqlTypeMappingSourceonly returned JSON mapping forstringorMySqlJsonStringCLR types, but EF Core requests mappings with the actual collection type (e.g.,List<Department>)
File: src/EFCore.MySql/Metadata/Conventions/MySqlJsonColumnConvention.cs
- Implements
IComplexPropertyAddedConventionandIComplexPropertyAnnotationChangedConvention - Detects when
.ToJson()is called by checking forJsonPropertyNameannotation - Uses
SetContainerColumnType("json")extension method (not genericHasAnnotation()) - Sets annotation on the complex type (not the complex property)
Key insight: Must use the specific extension method SetContainerColumnType() for the annotation to persist properly.
File: src/EFCore.MySql/Storage/Internal/MySqlTypeMappingSource.cs
Changed line 333 from:
if (storeTypeName.Equals("json", StringComparison.OrdinalIgnoreCase) &&
(clrType == null || clrType == typeof(string) || clrType == typeof(MySqlJsonString)))To:
if (storeTypeName.Equals("json", StringComparison.OrdinalIgnoreCase))Rationale: JSON can serialize any object, so the type mapping should work for any CLR type when store type is explicitly "json".
- MySQL: 5.7.8+ (native JSON type)
- MariaDB: 10.2.4+ (JSON functions, alias for LONGTEXT with validation)
Both support storing JSON in "json" column type.
Code changes complete and committed (615e974)
Remaining work:
- Verify convention is being triggered by EF Core convention pipeline
- Run actual failing tests with MySQL database to confirm fix
- May need to adjust convention timing if not triggering
The solution is theoretically sound based on investigation, but needs practical testing with the actual test suite.