{"record":{"id":"42e94d91f8a2fe21","repo":"dotnet/efcore","slug":"withpartitionkey-can-only-be-called-on-an-entity","errorCode":null,"errorMessage":"'WithPartitionKey' can only be called on an entity query root. See https://aka.ms/efdocs-cosmos-partition-keys for more information.","messagePattern":"'WithPartitionKey' can only be called on an entity query root\\. See https://aka\\.ms/efdocs-cosmos-partition-keys for more information\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs","lineNumber":175,"sourceCode":"        return base.Translate(expression);\n    }\n\n    /// <inheritdoc />\n    protected override Expression VisitMethodCall(MethodCallExpression methodCallExpression)\n    {\n        var method = methodCallExpression.Method;\n\n        if (methodCallExpression.Method.DeclaringType == typeof(CosmosQueryableExtensions)\n            && methodCallExpression.Method.Name == nameof(CosmosQueryableExtensions.WithPartitionKey))\n        {\n            if (_queryCompilationContext.PartitionKeyPropertyValues.Count > 0)\n            {\n                throw new InvalidOperationException(CosmosStrings.WithPartitionKeyAlreadyCalled);\n            }\n\n            if (methodCallExpression.Arguments[0] is not EntityQueryRootExpression)\n            {\n                throw new InvalidOperationException(CosmosStrings.WithPartitionKeyBadNode);\n            }\n\n            var innerQueryable = Visit(methodCallExpression.Arguments[0]);\n\n            for (var i = 1; i < methodCallExpression.Arguments.Count; i++)\n            {\n                var value = _sqlTranslator.Translate(methodCallExpression.Arguments[i], applyDefaultTypeMapping: false);\n                if (value is not SqlConstantExpression and not SqlParameterExpression)\n                {\n                    throw new InvalidOperationException(CosmosStrings.WithPartitionKeyNotConstantOrParameter);\n                }\n\n                _queryCompilationContext.PartitionKeyPropertyValues.Add(value);\n            }\n\n            return innerQueryable;\n        }\n","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs#L157-L193","documentation":"WithPartitionKey must sit directly on an EntityQueryRootExpression (a DbSet or entity query root). If Arguments[0] is not an EntityQueryRootExpression (for example, it follows a Where/Select/Join that rewrites the root) the visitor throws InvalidOperationException with WithPartitionKeyBadNode.","triggerScenarios":"Calling .Where(...).WithPartitionKey(k) or .Select(...).WithPartitionKey(k) — i.e. placing WithPartitionKey after operators that change the query root, instead of immediately on the DbSet.","commonSituations":"Applying WithPartitionKey inside a composed extension after a filter, or wrapping the DbSet in a helper that projects before adding the partition key.","solutions":["Place .WithPartitionKey(k) immediately after the DbSet, before any other LINQ operator.","If using a global query filter, set the partition key at query creation time on the raw DbSet.","Avoid wrapping the root in another IQueryable extension that returns a non-EntityQueryRootExpression node."],"exampleFix":"// before\nvar q = db.Users.Where(u => u.Active).WithPartitionKey(\"tenantA\");\n// after\nvar q = db.Users.WithPartitionKey(\"tenantA\").Where(u => u.Active);","handlingStrategy":"validation","validationCode":"static IQueryable<T> RootedPartition<T>(DbSet<T> set, object key)\n    where T : class\n    => set.WithPartitionKey(key);  // call on DbSet root only","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always apply WithPartitionKey as the first operator on a DbSet.","Code-review composition helpers that wrap IQueryable to ensure the partition call sits on the root.","Static-analyze for WithPartitionKey not preceded directly by a DbSet access."],"tags":["cosmos","partition-key","query-translation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}