{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs#L157-L193","documentation":"Thrown when WithPartitionKey is applied to a queryable that is not a direct entity query root (the first argument of the WithPartitionKey MethodCallExpression is not an EntityQueryRootExpression). The provider needs a raw DbSet root to inject partition-key filtering; applying it after LINQ operators (Where, Select, Join) destroys the root it can attach to.","triggerScenarios":"Calling WithPartitionKey after other LINQ operators: context.Blogs.Where(...).WithPartitionKey(\"A\"). Also when composing WithPartitionKey onto a projected or already-transformed queryable.","commonSituations":"Building a query with filters then appending partition key at the end. Reusable extension methods that apply WithPartitionKey late in the chain.","solutions":["Apply WithPartitionKey immediately on the DbSet before any other LINQ operator.","Reorder the query so the partition key is the first operator: context.Blogs.WithPartitionKey(\"A\").Where(...).","Ensure the queryable passed to WithPartitionKey is the DbSet itself, not a derived query."],"exampleFix":"// before\nvar q = context.Blogs.Where(b => b.Active).WithPartitionKey(\"tenantA\");\n// after\nvar q = context.Blogs.WithPartitionKey(\"tenantA\").Where(b => b.Active);","handlingStrategy":"validation","validationCode":"// Apply WithPartitionKey directly on the DbSet before composing\nIQueryable<Blog> q = context.Blogs.WithPartitionKey(tenantId);\nq = q.Where(b => b.Active);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always call WithPartitionKey immediately on the DbSet, before any other LINQ operator.","Review query-building helpers to ensure partition key is the first operator.","Add a unit test asserting WithPartitionKey is applied to a raw DbSet root."],"tags":["cosmos","partition-key","withpartitionkey","query","query-root"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}