Conversation
Copilot
AI
changed the title
[WIP] Fix ComplexCollection ToJson default value in migrations
Fix ComplexCollection ToJson migration default value: use "[]" instead of "{}"
Mar 21, 2026
roji
requested changes
Mar 21, 2026
Member
roji
left a comment
There was a problem hiding this comment.
Add a test covering the owned (non-complex) case, which you seem to have fixed as well.
Copilot stopped work on behalf of
roji due to an error
March 21, 2026 09:02
ec09fdb to
be88836
Compare
…stead of {}
Co-authored-by: roji <1862641+roji@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/1fd85ba2-cb75-4e30-b1f5-afbf64cc4626
Addresses review feedback: adds a test covering the owned (non-complex) collection case mapped to JSON. Owned collections are always nullable (IsUnique is false), so the default value is null rather than []. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
be88836 to
4823084
Compare
The IsJsonCollectionColumn check was incorrectly matching a nested
OwnsMany inside an OwnsOne reference, because the nested entity's
GetContainerColumnName() returns the root JSON column name and its
ownership has IsUnique: false. This caused Convert_string_column_to_
a_json_column_containing_required_reference to fail with '[]' instead
of '{}'.
Fix: only consider root-level entity mappings whose principal is not
itself mapped to JSON. Added a regression test for this case.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
roji
approved these changes
Mar 21, 2026
There was a problem hiding this comment.
Pull request overview
Fixes migration generation for ComplexCollection(...).ToJson() so non-nullable JSON collection columns get an empty JSON array default ("[]") rather than an empty JSON object ("{}"), preventing runtime failures when materializing entities from the default value.
Changes:
- Update
MigrationsModelDiffer.InitializeJsonColumnto choose"[]"vs"{}"based on whether the JSON column represents a collection. - Add
IsJsonCollectionColumn(JsonColumn)helper to detect collection-backed JSON container columns. - Add migration-diff tests covering the complex-collection scenario (and additional JSON owned-type scenarios).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs | Adjusts default JSON value generation for non-nullable JSON container columns, introducing collection detection. |
| test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs | Adds regression coverage asserting "[]" default for complex collections mapped to JSON and validating behaviors for owned JSON mappings. |
src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs
Outdated
Show resolved
Hide resolved
…lex references
was already applied to the owned entity branch. Without this, a nested
ComplexCollection inside a ComplexProperty (reference) would falsely
classify the root JSON column as a collection, producing '[]' instead
of '{}'. Added regression test.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
AndriySvyryd
approved these changes
Mar 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When adding a
ComplexCollectionmapped withToJson()to an existing table, the generated migration incorrectly setsdefaultValue: "{}"(JSON object) instead ofdefaultValue: "[]"(JSON array), causing a runtimeInvalidOperationExceptionwhen reading entities with the default value.Root Cause
MigrationsModelDiffer.InitializeJsonColumnunconditionally used"{}"as the default for all non-nullable JSON columns, without distinguishing collections from single objects.Changes
MigrationsModelDiffer.cs: AddedIsJsonCollectionColumnhelper that inspects the table'sComplexTypeMappingsandEntityTypeMappingsto detect collection-backed JSON columns:ComplexProperty.IsCollection == trueandGetContainerColumnName()matches the columnOwnsMany): ownershipIsUnique: false"[]"as default for collection columns,"{}"for single-object columnsMigrationsModelDifferTest.cs: AddedAdd_complex_collection_mapped_to_json_uses_empty_array_as_default_valuetest covering the reported scenario.Original prompt
This section details on the original issue you should resolve
<issue_title>ComplexCollection ToJson produces a
{}default value instead of[]in migrations</issue_title><issue_description>### Bug description
e.ComplexCollection(m => m.MyCollection, x => x.ToJson());to an existing entity.defaultValuein the migration is{}instead of[].Your code
Stack traces
Attempts to read the entity after the incorrect migration runs results in the following due to JSON objects being present where JSON arrays are expected:
Verbose output
EF Core version
10.0.5
Database provider
Microsoft.EntityFrameworkCore.SqlServer
Target framework
.NET 10
Operating system
Windows 11
IDE
VS Code</issue_description>
Comments on the Issue (you are @copilot in this section)
@roji # AI TriageThe below is an AI-generated analysis and may contain inaccuracies.
Summary
Confirmed bug. When adding a
ComplexCollectionmapped withToJson()to an existing entity via migrations, the generated migration setsdefaultValue: "{}"(JSON object) instead ofdefaultValue: "[]"(JSON array). This causes a runtimeInvalidOperationException("Invalid token type: 'StartObject'") when reading entities with the default value.Root Cause
In
MigrationsModelDiffer.InitializeJsonColumn, the default value is unconditionally set to"{}"for all non-nullable, non-inline JSON columns:This does not distinguish between complex properties (JSON objects →
"{}"is correct) and complex collections (JS...{}default value instead of[]in migrations #37964🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.