{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L459-L495","documentation":"Thrown by ValidateKeys when the partition-key property's effective type (ClrType after any value converter, unwrapped from Nullable<T>) is not string, a numeric type, or bool. Cosmos partition keys support only those primitive types, so EF rejects unsupported partition-key property types at validation.","triggerScenarios":"Configuring a Guid, DateTime, enum, or complex-typed property as the partition key without converting it to a numeric/string/bool provider type.","commonSituations":"Using a natural-key Guid or DateTime as the partition key; enums as partition keys; forgetting that the property type must match Cosmos's supported set.","solutions":["Choose a string, numeric, or bool property as the partition key.","Add a value converter to a supported type, e.g. .HasConversion(v => v.ToString(), v => Guid.Parse(v)) for a Guid partition key.","Add a computed/derived partition-key property of a supported type and partition on that."],"exampleFix":"// before\nmodelBuilder.Entity<Event>().HasPartitionKey(e => e.Timestamp); // DateTime -> rejected\n\n// after\nmodelBuilder.Entity<Event>()\n    .Property(e => e.DayKey).HasConversion<int>();\nmodelBuilder.Entity<Event>().HasPartitionKey(e => e.DayKey);","handlingStrategy":"validation","validationCode":"using var ctx = new MyContext();\nforeach (var e in ctx.Model.GetEntityTypes())\n{\n    foreach (var name in e.GetPartitionKeyPropertyNames())\n    {\n        var p = e.FindProperty(name);\n        if (p is null) continue;\n        var t = (p.GetTypeMapping().Converter?.ProviderClrType ?? p.ClrType).UnwrapNullableType();\n        Debug.Assert(t == typeof(string) || t == typeof(bool) || t.IsNumeric(),\n            $\"{e.Name}.{name} partition key type {t} is not numeric/bool/string\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try { ctx.Model.GetModel(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"partition key property types must be numeric\"))\n{\n    logger.LogError(ex, \"Partition key property has an unsupported type\");\n    throw;\n}","preventionTips":["Choose a string, numeric, or bool property as the partition key.","Add a value converter to a supported type for Guid/DateTime/enum keys.","Test the effective provider type of each partition-key property."],"tags":["cosmos","configuration","model-validation","partition-key","value-converter"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}