Owned JSON collection navigation absent from the stored document materializes as null (owned counterpart of #38625)
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 65/100
Research direction
Start with JsonEntityMaterializerRewriter and compare the absent-property fixup from #39014 with the handling of INavigation collections. Use the existing JsonEntityBasic fixture with SQLite and shaped documents to cover absent and explicit-null keys in tracking and no-tracking queries. Done means an absent owned collection materializes as empty while explicit JSON null remains null.
Written by the indexing model from the issue text.
Description
Bug description
#38625 reports that a complex collection mapped to JSON whose key is absent from the stored document is materialized as null rather than an empty collection, so rows persisted before the collection was added to the type become unusable. #39014 fixes that for complex collections and, on purpose, leaves owned entity collection navigations mapped to JSON alone to keep the change to the reported scenario.
The owned case has exactly the same behavior, from the same code path: JsonEntityMaterializerRewriter runs every nested-property fixup after the read loop, so a navigation that never appeared in the document has its fixup invoked with null.
Reproduced on main with the existing JsonEntityBasic fixture (SQLite, json_remove/json_set to shape the stored document):
Stored OwnedReferenceRoot document |
AsNoTracking() |
tracking |
|---|---|---|
key OwnedCollectionBranch absent |
OwnedCollectionBranch == null |
null |
key present with [] |
empty list | empty list |
So an application that adds an owned collection to an existing JSON-mapped owned type sees null for every pre-existing row, but [] for every row written afterwards. Documents written by EF always contain the key, so this only bites after schema evolution or with documents produced by another writer.
Expected behavior
Consistent with the fix for complex collections in #39014: an absent owned collection navigation materializes as an empty collection; an explicit JSON null is preserved.
Precedent within EF
- Cosmos already does this.
CosmosShapedQueryCompilingExpressionVisitorcreates a nested collection withcollectionAccessor.Create()before its property read loop and adds elements as they are read (source), so a key that never appears yields an empty collection. (Cosmos also yields empty for an explicitnull; the relational fix in #39014 keepsnullthere, which seems the better distinction, but the absent-key outcome is the same.) - Relational already produces the same object for a present
[].MaterializeJsonEntityCollectionbuilds the empty collection through the property'sIClrCollectionAccessor; the fix in #39014 uses the same accessor for the absent case, so absent and[]become indistinguishable, which is what a schema-evolved row needs.
Notes for the fix
The mechanism from #39014 extends directly: register the absent-property fixup for INavigation { IsCollection: true } as well as IComplexProperty. Two details differ from the complex case and are worth a decision:
- Tracking queries. For owned entities the change tracker performs fixup, and the existing tracked fixup only assigns the collection when it is
nullor empty. An absent key should produce the same result as a present[], which the tracked path already assigns, so the absent fixup can assign the empty collection unconditionally. - Behavioral change. Unlike complex collections (new in 10.0), owned JSON collections have materialized
nullfor an absent key since JSON columns shipped in EF 7. Anyone relying onnullhere would see[]instead.
I'm happy to do this either as an extension of #39014 or as a separate PR once that one lands; which would the team prefer?
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.5k
- Avg merge
- 3d 7h
- Merged PRs (30d)
- 182
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
customer-reported
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
GroupBy element `OrderBy(...).First()`/`MaxBy` over an owned type mapped to its own table fails to translatePossibly taken @cincuranet claimed this today. Openarea-groupby area-query
Difficulty 4/5 3-5 days Newbie friendliness 25/100
dotnet/efcore#39833 · 1 assignee ·
Maintainers usually reply within 2 days
-
SQLite: Polygon.Length translates to GLength, which returns 0Possibly taken @cincuranet claimed this today. Openarea-query area-sqlite
Difficulty 3/5 1-2 days Newbie friendliness 45/100
dotnet/efcore#39826 · 1 assignee ·
Maintainers usually reply within 2 days
-
Migration lock with LockReleaseBehavior.Transaction doesn't prevent concurrent migrations since per-migration transactionsPossibly taken @AndriySvyryd claimed this 1 day ago. Openarea-migrations
dotnet/efcore#39824 · 1 assignee ·
Maintainers usually reply within 2 days
-
Querying a TPC base type with a JSON complex property throwsPossibly taken @cincuranet claimed this 1 day ago. Openarea-query
Difficulty 4/5 3-5 days Newbie friendliness 22/100
dotnet/efcore#39814 · 1 assignee ·
Maintainers usually reply within 2 days
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
SubtitleEdit/subtitleedit#17247 ·
Maintainers usually reply within 1 day
-
Restore test cleanup fails on Windows when restored files are read-onlyPossibly taken @JamBalaya56562 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
duplicati/duplicati#8046 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
Bundles Documentation
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
pyrevitlabs/pyRevit#4285 ·
Maintainers usually reply within 1 day