Owned JSON collection navigation absent from the stored document materializes as null (owned counterpart of #38625)

Abierto
#39,060 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
65/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
csharp, sqlite
Área
databases

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

area-json area-owned-entities area-query
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. CosmosShapedQueryCompilingExpressionVisitor creates a nested collection with collectionAccessor.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 explicit null; the relational fix in #39014 keeps null there, which seems the better distinction, but the absent-key outcome is the same.)
  • Relational already produces the same object for a present []. MaterializeJsonEntityCollection builds the empty collection through the property's IClrCollectionAccessor; 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 null or 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 null for an absent key since JSON columns shipped in EF 7. Anyone relying on null here 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?

Lenguaje dominante
C#
Estrellas
14.8k
Forks
3.5k
Merge medio
3 d 7 h
PR fusionados (30 d)
182

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de dotnet/efcore

Todos los issues de dotnet/efcore

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.