{"record":{"id":"bb6876892831ec90","repo":"dotnet/efcore","slug":"navigation-from-json-mapped-entity-jsonentity","errorCode":null,"errorMessage":"Navigation from JSON-mapped entity '{jsonEntity}' to its parent entity '{parentEntity}' using navigation '{navigation}' is not supported. Entities mapped to JSON can only navigate to their children.","messagePattern":"Navigation from JSON-mapped entity '(.+?)' to its parent entity '(.+?)' using navigation '(.+?)' is not supported\\. Entities mapped to JSON can only navigate to their children\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Query/JsonQueryExpression.cs","lineNumber":171,"sourceCode":"    /// <param name=\"structuralProperty\">The navigation or complex property to bind.</param>\n    /// <returns>An JSON query expression for the target entity or complex type.</returns>\n    public virtual JsonQueryExpression BindStructuralProperty(IPropertyBase structuralProperty)\n    {\n        switch (structuralProperty)\n        {\n            case INavigation navigation:\n            {\n                if (StructuralType is not IEntityType entityType)\n                {\n                    throw new UnreachableException(\"Navigation on complex JSON type\");\n                }\n\n                Check.DebugAssert(KeyPropertyMap is not null);\n\n                if (navigation.ForeignKey.DependentToPrincipal == navigation)\n                {\n                    // issue #28645\n                    throw new InvalidOperationException(\n                        RelationalStrings.JsonCantNavigateToParentEntity(\n                            navigation.ForeignKey.DeclaringEntityType.DisplayName(),\n                            navigation.ForeignKey.PrincipalEntityType.DisplayName(),\n                            navigation.Name));\n                }\n\n                var targetEntityType = navigation.TargetEntityType;\n                var newPath = Path.ToList();\n                newPath.Add(new PathSegment(GetJsonElement(navigation).PropertyName!));\n\n                var newKeyPropertyMap = new Dictionary<IProperty, ColumnExpression>();\n                var targetPrimaryKeyProperties = targetEntityType.FindPrimaryKey()!.Properties.Take(KeyPropertyMap.Count);\n                var sourcePrimaryKeyProperties = entityType.FindPrimaryKey()!.Properties.Take(KeyPropertyMap.Count);\n                foreach (var (target, source) in targetPrimaryKeyProperties.Zip(sourcePrimaryKeyProperties, (t, s) => (t, s)))\n                {\n                    newKeyPropertyMap[target] = KeyPropertyMap[source];\n                }\n","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Query/JsonQueryExpression.cs#L153-L189","documentation":"Thrown by JsonQueryExpression.BindStructuralProperty when attempting to traverse a dependent-to-principal navigation (a navigation back to the parent entity) from an entity mapped to JSON. EF Core only supports navigating from parent to children in JSON mappings, not upward from JSON entities to their parents (tracked as issue #28645).","triggerScenarios":"A LINQ query navigates from a JSON-mapped owned entity back to its parent: e.g., context.Customers.SelectMany(c => c.Addresses).Where(a => a.Customer.Name == ...) where Addresses is JSON-mapped and Customer is the parent navigation. The navigation.ForeignKey.DependentToPrincipal check catches this reverse traversal.","commonSituations":"Querying JSON-mapped owned collections and filtering/projecting through the parent navigation. Using a JSON-mapped entity in a join or subquery that requires resolving the parent reference. Moving a navigation that was previously table-based into JSON mapping without adjusting queries.","solutions":["Rewrite the query to traverse from parent to child instead of child to parent: filter on the parent entity directly.","Move the navigation out of JSON mapping (use a regular FK relationship instead of ToJson) if bidirectional navigation is needed.","If you need to query the JSON entity independently, project the parent's key into the JSON document as a scalar and query by that.","Track the issue (#28645) for future EF Core support of upward JSON navigation."],"exampleFix":"// before — navigating from JSON child back to parent\nvar query = context.Customers\n    .SelectMany(c => c.Addresses) // Addresses is ToJson-mapped\n    .Where(a => a.Customer.Name == \"Acme\"); // Customer is parent nav → throws\n\n// after — query from parent instead\nvar query = context.Customers\n    .Where(c => c.Name == \"Acme\")\n    .SelectMany(c => c.Addresses);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    var result = await context.Customers\n        .SelectMany(c => c.Addresses)\n        .Where(a => a.Customer.Name == \"Acme\")\n        .ToListAsync();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not supported\") && ex.Message.Contains(\"JSON\"))\n{\n    // Rewrite: query from parent instead of from JSON child\n    var result = await context.Customers\n        .Where(c => c.Name == \"Acme\")\n        .SelectMany(c => c.Addresses)\n        .ToListAsync();\n}","preventionTips":["Always traverse JSON relationships from parent to child, never child to parent.","Review all LINQ queries that touch JSON-mapped owned types for reverse navigation patterns.","Document which navigations are JSON-mapped so developers know to avoid upward traversal.","Consider using regular FK relationships (not ToJson) if bidirectional navigation is required."],"tags":["json-mapping","navigation","dependent-to-principal","limitation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}