{"record":{"id":"464a038635598622","repo":"dotnet/efcore","slug":"withpartitionkey-only-accepts-simple-constant-or","errorCode":null,"errorMessage":"'WithPartitionKey' only accepts simple constant or parameter arguments. See https://aka.ms/efdocs-cosmos-partition-keys for more information.","messagePattern":"'WithPartitionKey' only accepts simple constant or parameter arguments\\. 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":185,"sourceCode":"        {\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\n        if (method.DeclaringType == typeof(Queryable) && method.IsGenericMethod)\n        {\n            switch (methodCallExpression.Method.Name)\n            {\n                // The following is a bad hack to account for https://github.com/dotnet/efcore/issues/32957#issuecomment-2165864086.\n                // Basically for the query form Where(b => b.Posts.GetElementAt(0).Id == 1), nav expansion moves the property access\n                // forward, generating Where(b => b.Posts.Select(p => p.Id).GetElementAt(0)); unfortunately that means that GetElementAt()\n                // over a bare array in Cosmos doesn't get translated to a simple indexer as it should (b[\"Posts\"][0].Id), since the\n                // reordering messes things up.\n                case nameof(Queryable.ElementAt) or nameof(Queryable.ElementAtOrDefault)","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs#L167-L203","documentation":"Thrown when an argument passed to WithPartitionKey is not a simple constant or parameter expression. The provider translates partition key values directly into Cosmos SDK PartitionKey construction, which requires concrete values known at execution time; computed expressions, method calls, or member accesses cannot be turned into a partition key without evaluation.","triggerScenarios":"Passing a computed value, a property of another entity, or a method-call result to WithPartitionKey, e.g. WithPartitionKey(someObject.TenantId) where someObject.TenantId is not a captured constant or a query parameter.","commonSituations":"Reading the partition key from a config object's instance property rather than a captured local or a parameter. Passing an expression that depends on query state.","solutions":["Extract the value into a local variable or parameter before calling WithPartitionKey: var tenant = config.TenantId; context.Blogs.WithPartitionKey(tenant).","If the value comes from a service, resolve it to a concrete value first and pass that.","Ensure the argument is a literal, a captured constant, or an EF query parameter."],"exampleFix":"// before\nvar q = context.Blogs.WithPartitionKey(_config.CurrentTenant.Id);\n// after\nvar tenantId = _config.CurrentTenant.Id;\nvar q = context.Blogs.WithPartitionKey(tenantId);","handlingStrategy":"validation","validationCode":"// Extract values into locals/parameters before passing to WithPartitionKey\nvar tenantId = _config.CurrentTenant.Id;\nif (string.IsNullOrEmpty(tenantId)) throw new InvalidOperationException(\"TenantId required\");\nvar q = context.Blogs.WithPartitionKey(tenantId);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass only literals, captured constants, or EF parameters to WithPartitionKey.","Resolve dynamic values into locals before the query call.","Avoid property/member-access expressions as partition key arguments."],"tags":["cosmos","partition-key","withpartitionkey","query","constant","parameter"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}