{"record":{"id":"e65f3aafab352e1a","repo":"dotnet/efcore","slug":"the-type-of-the-partition-key-property-property","errorCode":null,"errorMessage":"The type of the partition key property '{property}' on '{entityType}' is '{propertyType}'. All partition key property types must be numeric, Boolean, or string, or converted to one of these types.","messagePattern":"The type of the partition key property '(.+?)' on '(.+?)' is '(.+?)'\\. All partition key property types 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/Infrastructure/Internal/CosmosModelValidator.cs","lineNumber":477,"sourceCode":"                    CosmosStrings.PartitionKeyNotOnRoot(entityType.DisplayName(), entityType.BaseType.DisplayName()));\n            }\n\n            foreach (var partitionKeyPropertyName in partitionKeyPropertyNames)\n            {\n                var partitionKey = entityType.FindProperty(partitionKeyPropertyName);\n                if (partitionKey == null)\n                {\n                    throw new InvalidOperationException(\n                        CosmosStrings.PartitionKeyMissingProperty(entityType.DisplayName(), partitionKeyPropertyName));\n                }\n\n                var partitionKeyType = (partitionKey.GetTypeMapping().Converter?.ProviderClrType\n                    ?? partitionKey.ClrType).UnwrapNullableType();\n                if (partitionKeyType != typeof(string)\n                    && !partitionKeyType.IsNumeric()\n                    && partitionKeyType != typeof(bool))\n                {\n                    throw new InvalidOperationException(\n                        CosmosStrings.PartitionKeyBadStoreType(\n                            partitionKeyPropertyName,\n                            entityType.DisplayName(),\n                            partitionKeyType.ShortDisplayName()));\n                }\n            }\n        }\n    }\n\n    /// <summary>\n    ///     Validates that a key doesn't have mutable properties.\n    /// </summary>\n    /// <param name=\"key\">The key to validate.</param>\n    /// <param name=\"logger\">The logger to use.</param>\n    protected override void ValidateMutableKey(\n        IKey key,\n        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)\n    {","sourceCodeStart":459,"sourceCodeEnd":495,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L459-L495","documentation":"Thrown when a partition key property's effective store type is not string, numeric, or Boolean. Cosmos partition keys must be one of those primitive types (or a value converter to one of them). The validator unwrapping-nullable on the converter's ProviderClrType (or ClrType) and rejects anything else (e.g. Guid, DateTimeOffset, byte[]).","triggerScenarios":"Configuring HasPartitionKey on a property whose ClrType is Guid, DateTime, DateTimeOffset, byte[], enum (without conversion), or any custom struct, without a value converter to a numeric/string/bool type. Fires during ValidateKeys.","commonSituations":"Partitioning by a Guid id directly; partitioning by a DateTime column; using an enum as the partition key without a numeric/string converter; new partition key on a property whose type was chosen for business logic, not Cosmos compatibility.","solutions":["Add a value converter that maps the property to string or a numeric type: .HasConversion<string>() (most common) or .HasConversion<long>().","Choose a different partition-key property that is already string/numeric/bool.","For enums, use HasConversion<int>() or HasConversion<string>().","For Guids, HasConversion<string>() is the typical fix."],"exampleFix":"// before\nmodelBuilder.Entity<Event>().HasPartitionKey(e => e.EventId); // Guid\n\n// after\nmodelBuilder.Entity<Event>()\n    .Property(e => e.EventId).HasConversion<string>();\nmodelBuilder.Entity<Event>().HasPartitionKey(e => e.EventId);","handlingStrategy":"validation","validationCode":"foreach (var et in dbContext.Model.GetEntityTypes())\n{\n    foreach (var name in et.GetPartitionKeyPropertyNames())\n    {\n        var p = et.FindProperty(name);\n        if (p is null) continue;\n        var t = (p.GetTypeMapping().Converter?.ProviderClrType ?? p.ClrType).UnwrapNullableType();\n        if (t != typeof(string) && !t.IsNumeric() && t != typeof(bool))\n            throw new InvalidOperationException($\"{name} partition key type {t} not allowed\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Convert Guid/Date/enum partition key properties with HasConversion<string>() or HasConversion<int>().","Prefer string or long for partition keys.","Add a model-validation unit test asserting partition key store types."],"tags":["cosmos","model-validation","partition-key","type-conversion","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}