{"record":{"id":"723c191c9b1b5d36","repo":"dotnet/efcore","slug":"the-partition-key-value-supplied-for-propertytyp","errorCode":null,"errorMessage":"The partition key value supplied for '{propertyType}' property '{entityType}.{property}' is of type '{valueType}'. Partition key values must be of a type assignable to the property.","messagePattern":"The partition key value supplied for '(.+?)' property '(.+?)\\.(.+?)' is of type '(.+?)'\\. Partition key values must be of a type assignable to the property\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Extensions/Internal/PartitionKeyBuilderExtensions.cs","lineNumber":85,"sourceCode":"\n                case var _ when value.GetType().IsNumeric():\n                    if (expectedType != null && !expectedType.IsNumeric())\n                    {\n                        CheckType(value.GetType());\n                    }\n\n                    builder.Add(Convert.ToDouble(value));\n                    break;\n\n                default:\n                    throw new InvalidOperationException(CosmosStrings.PartitionKeyBadValue(value.GetType()));\n            }\n\n            void CheckType(Type actualType)\n            {\n                if (expectedType != null && expectedType != actualType)\n                {\n                    throw new InvalidOperationException(\n                        CosmosStrings.PartitionKeyBadValueType(\n                            expectedType.ShortDisplayName(),\n                            property!.DeclaringType.DisplayName(),\n                            property.Name,\n                            actualType.DisplayName()));\n                }\n            }\n        }\n\n        return builder;\n    }\n}\n","sourceCodeStart":67,"sourceCodeEnd":98,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Extensions/Internal/PartitionKeyBuilderExtensions.cs#L67-L98","documentation":"After value conversion, PartitionKeyBuilderExtensions.Add checks that the runtime value's CLR type matches the partition key property's expected type via the local CheckType function; a mismatch throws InvalidOperationException via CosmosStrings.PartitionKeyBadValueType. E.g. passing a string value for a partition key property typed int.","triggerScenarios":"Calling WithPartitionKey with a value whose type differs from the declared partition key property type (e.g. WithPartitionKey(\"123\") when the property is int); a converter that changes the provider type such that the runtime value no longer matches.","commonSituations":"Loosely typed WithPartitionKey(object) call with a boxed value of the wrong type; refactoring the partition key property type without updating callers; a value converter that yields a different provider type.","solutions":["Pass a value whose type matches the partition key property's CLR (or provider) type exactly.","Re-check the property type declared via HasPartitionKey and align the WithPartitionKey argument to it.","If a converter is in play, pass a value of the converter's ProviderClrType, not the original CLR type."],"exampleFix":"// before (string passed for an int partition key property)\nvar q = context.Items.WithPartitionKey(\"123\");\n\n// after (int matches the declared partition key property)\nvar q = context.Items.WithPartitionKey(123);","handlingStrategy":"validation","validationCode":"// Ensure the passed value matches the partition key property's CLR type.\nvar pkProp = context.Model.FindEntityType(typeof(Item))!\n    .GetPartitionKeyPropertyNames().First();\nvar expected = context.Model.FindEntityType(typeof(Item))!.FindProperty(pkProp)!.ClrType;\nif (value != null && !expected.IsAssignableFrom(value.GetType()))\n    throw new ArgumentException($\"Partition key value must be {expected.Name}.\");\nreturn context.Items.WithPartitionKey(value);","typeGuard":"static bool MatchesPartitionType(IModel model, Type entity, object? value)\n{\n    var pk = model.FindEntityType(entity)!.GetPartitionKeyPropertyNames().FirstOrDefault();\n    if (pk is null) return true;\n    var t = model.FindEntityType(entity)!.FindProperty(pk)!.ClrType;\n    return value is null || t.IsAssignableFrom(value.GetType());\n}","tryCatchPattern":"try { return context.Items.WithPartitionKey(value).ToList(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Partition key values must be\"))\n{ throw new ArgumentException($\"Pass a value of the partition key property's type.\", ex); }","preventionTips":["Pass a value whose type matches the declared partition key property.","After changing the property type, update all WithPartitionKey callers.","When a value converter is present, pass the provider-side type, not the CLR type."],"tags":["cosmos","partition-key","type-mismatch","value-conversion"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}