{"record":{"id":"652774e1349f7556","repo":"dotnet/efcore","slug":"the-partition-key-value-is-of-type-valuetype-w","errorCode":null,"errorMessage":"The partition key value is of type '{valueType}' which is not valid for Cosmos partition keys. All partition key properties values must be numeric, Boolean, or string, or converted to one of these types.","messagePattern":"The partition key value is of type '(.+?)' which is not valid for Cosmos partition keys\\. All partition key properties values must be numeric, Boolean, or string, or converted to one of these types\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Extensions/Internal/PartitionKeyBuilderExtensions.cs","lineNumber":78,"sourceCode":"                    if (expectedType != null && expectedType != typeof(bool))\n                    {\n                        CheckType(typeof(bool));\n                    }\n\n                    builder.Add(boolValue);\n                    break;\n\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    }","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Extensions/Internal/PartitionKeyBuilderExtensions.cs#L60-L96","documentation":"PartitionKeyBuilderExtensions.Add throws InvalidOperationException(CosmosStrings.PartitionKeyBadValue) when the value passed (after the property's value converter runs) is neither a string, a bool, nor a numeric type. Cosmos partition keys only support those primitive CLR categories; anything else (Guid, DateTime, enum-as-object, byte[], custom struct) hits the default switch arm and is rejected.","triggerScenarios":"Calling PartitionKeyBuilder.Add with a Guid, DateTimeOffset, byte[], or custom struct value where a partition key is expected; a value converter that produces a non-numeric/non-string/non-bool provider value; an enum value boxed as object that did not get converted to its underlying numeric; passing a property whoseClrType is not one of the supported categories and has no converter.","commonSituations":"Modeling a partition key on a Guid or DateTime property without a value converter to string/numeric; using a custom struct for partition keys; converters that emit unsupported provider types; runtime data that drifts type from the configured property type.","solutions":["Configure a value converter on the partition key property that maps it to string, bool, or a numeric type (e.g. Guid -> string via ToString).","Use a partition key property whose ClrType is already string, bool, int, long, double, decimal, etc.","If the value is an enum, make sure the property type is the enum (so Add can convert it to its numeric value) rather than passing the boxed enum as object.","Validate the value type before calling Add and reject or coerce unsupported types in your own code."],"exampleFix":"// before - Guid partition key value throws\nbuilder.Add(myGuid, partitionKeyProperty);\n\n// after - convert the property to string at the model level\nentity.Property(e => e.Id)\n    .HasConversion(g => g.ToString(), s => Guid.Parse(s));\n// or convert at the call site\nbuilder.Add(myGuid.ToString(), partitionKeyProperty);","handlingStrategy":"validation","validationCode":"// Validate the partition key value before building it.\nstatic bool IsSupportedPartitionKeyType(object? value)\n    => value is null || value is string || value is bool || value.GetType().IsNumeric();\n\nif (!IsSupportedPartitionKeyType(value))\n    throw new ArgumentException($\"Unsupported partition key type {value?.GetType()}\");\nbuilder.Add(value, property);","typeGuard":"static bool IsSupportedPartitionKeyType(Type t)\n    => t == typeof(string) || t == typeof(bool) || t.IsNumeric();","tryCatchPattern":null,"preventionTips":["Use partition key properties of type string, bool, or numeric only.","Configure a value converter on non-supported types (Guid, DateTime) to a supported primitive.","Validate partition key values at the application boundary, not just at EF.","Add a unit test that exercises partition key building for every entity type."],"tags":["ef-core","cosmos","partition-key","type-conversion","validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}