{"record":{"id":"71dc2529bffec15e","repo":"dotnet/efcore","slug":"the-partition-key-for-entity-type-entitytype-i","errorCode":null,"errorMessage":"The partition key for entity type '{entityType}' is set to '{property}', but there is no property with that name.","messagePattern":"The partition key for entity type '(.+?)' is set to '(.+?)', but there is no property with that name\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs","lineNumber":467,"sourceCode":"        if (partitionKeyPropertyNames.Count == 0)\n        {\n            logger.NoPartitionKeyDefined(entityType);\n        }\n        else\n        {\n            if (entityType.BaseType != null\n                && entityType.FindAnnotation(CosmosAnnotationNames.PartitionKeyNames)?.Value != null)\n            {\n                throw new InvalidOperationException(\n                    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    }","sourceCodeStart":449,"sourceCodeEnd":485,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L449-L485","documentation":"Thrown by ValidateKeys when a name returned by GetPartitionKeyPropertyNames() does not resolve to any property via entityType.FindProperty(name). The partition-key annotation lists property names that must exist on the entity; a dangling name means HasPartitionKey referenced a property that was never declared or was removed.","triggerScenarios":"Calling HasPartitionKey(\"RegionId\") (string overload) when no property named RegionId exists, or setting partition key property names directly via SetPartitionKeyPropertyNames with a typo.","commonSituations":"Renaming a property but not the partition-key reference; using the string-based overload with a mismatched casing/spelling; setting the partition key before defining the property.","solutions":["Use the lambda overload HasPartitionKey(e => e.RegionId) so the compiler verifies the property exists.","If using the string overload, ensure the name exactly matches a declared property (case-sensitive).","Declare the property before configuring it as the partition key."],"exampleFix":"// before\nmodelBuilder.Entity<Order>().HasPartitionKey(\"RegionId\"); // property is Region\n\n// after\nmodelBuilder.Entity<Order>().HasPartitionKey(o => o.Region);","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        Debug.Assert(e.FindProperty(name) is not null, $\"{e.Name}: partition key property '{name}' does not exist\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try { ctx.Model.GetModel(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"there is no property with that name\"))\n{\n    logger.LogError(ex, \"Partition key references a non-existent property\");\n    throw;\n}","preventionTips":["Prefer the lambda overload HasPartitionKey(e => e.X) so the compiler validates the member.","If using the string overload, ensure the name matches a declared property exactly.","Declare partition-key properties before referencing them."],"tags":["cosmos","configuration","model-validation","partition-key"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}