|
| 1 | +# Complex Collection JSON Mapping Support |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Starting with EF Core 10, complex collections (collections of complex types within an entity) **must** be mapped to JSON columns. This document explains how to use this feature with the Pomelo MySQL provider. |
| 6 | + |
| 7 | +## What are Complex Collections? |
| 8 | + |
| 9 | +Complex collections are properties on an entity that contain a collection of complex types (value objects), not entities. For example: |
| 10 | + |
| 11 | +```csharp |
| 12 | +public class School |
| 13 | +{ |
| 14 | + public int Id { get; set; } |
| 15 | + public string Name { get; set; } |
| 16 | + |
| 17 | + // This is a complex collection |
| 18 | + public List<Department> Departments { get; set; } |
| 19 | +} |
| 20 | + |
| 21 | +// This is a complex type (not an entity - no key) |
| 22 | +public class Department |
| 23 | +{ |
| 24 | + public string Name { get; set; } |
| 25 | + public int Budget { get; set; } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## Database Version Requirements |
| 30 | + |
| 31 | +JSON column support requires: |
| 32 | +- **MySQL 5.7.8 or later** (native JSON type support) |
| 33 | +- **MariaDB 10.2.4 or later** (JSON functions support) |
| 34 | + |
| 35 | +## Configuration |
| 36 | + |
| 37 | +In EF Core 10, complex collections must be explicitly mapped to JSON columns: |
| 38 | + |
| 39 | +```csharp |
| 40 | +protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 41 | +{ |
| 42 | + modelBuilder.Entity<School>(entity => |
| 43 | + { |
| 44 | + entity.HasKey(e => e.Id); |
| 45 | + |
| 46 | + // Configure complex collection to use JSON column |
| 47 | + // The exact API is: ComplexProperty(e => e.Departments) |
| 48 | + // Note: The specific configuration method may vary |
| 49 | + entity.ComplexProperty(e => e.Departments); |
| 50 | + }); |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## Error Without Proper Configuration |
| 55 | + |
| 56 | +If you don't configure a complex collection to use JSON, you'll get this error: |
| 57 | + |
| 58 | +``` |
| 59 | +System.InvalidOperationException: The complex collection property 'School.Departments' |
| 60 | +must be mapped to a JSON column. Use 'ToJson()' to configure this complex collection |
| 61 | +as mapped to a JSON column. |
| 62 | +``` |
| 63 | + |
| 64 | +## Testing |
| 65 | + |
| 66 | +Tests that use complex collections should use the version check attribute to skip on older database versions: |
| 67 | + |
| 68 | +```csharp |
| 69 | +[SupportedServerVersionCondition("Json")] |
| 70 | +public class MyComplexCollectionTests |
| 71 | +{ |
| 72 | + // Tests here will only run on MySQL 5.7.8+ or MariaDB 10.2.4+ |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +## Implementation Details |
| 77 | + |
| 78 | +The Pomelo provider now: |
| 79 | +1. Allows JSON-mapped entities (previously blocked with an error) |
| 80 | +2. Delegates validation to EF Core's base `RelationalModelValidator` |
| 81 | +3. Supports complex collections mapped to JSON columns |
| 82 | + |
| 83 | +## Migration from Earlier Versions |
| 84 | + |
| 85 | +If you're upgrading from an earlier EF Core version: |
| 86 | +1. Ensure your database version supports JSON (MySQL 5.7.8+ or MariaDB 10.2.4+) |
| 87 | +2. Complex collections that worked with table splitting in earlier versions now require JSON mapping |
| 88 | +3. Update your model configuration to explicitly map complex collections |
| 89 | + |
| 90 | +## References |
| 91 | + |
| 92 | +- [EF Core Complex Types Documentation](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-10.0/whatsnew#complex-types) |
| 93 | +- [MySQL JSON Support](https://dev.mysql.com/doc/refman/8.0/en/json.html) (5.7.8+) |
| 94 | +- [MariaDB JSON Functions](https://mariadb.com/kb/en/json-functions/) (10.2.4+) |
0 commit comments