{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L449-L485","documentation":"Thrown when the partition key references a property name that does not exist on the entity type. The validator iterates the partition key property names and calls entityType.FindProperty(name); a null result means no property is declared with that name, so the partition key cannot be honored.","triggerScenarios":"Calling HasPartitionKey(e => e.SomeField) where SomeField is misspelled, was renamed, or never declared on the entity (or its base types). Also triggered by string-based HasPartitionKey(\"WrongName\"). Fires during ValidateKeys.","commonSituations":"Renaming a property in code but forgetting to update the partition-key mapping; using the JSON property name instead of the CLR property name; copying configuration between entities that have differently named fields; typos in the lambda or string.","solutions":["Verify the property name in the HasPartitionKey lambda matches a real property on the root entity (use the lambda form, not a magic string, to get compile-time safety).","If the property was renamed, update the partition-key mapping to the new name.","Ensure the property is declared on the root entity type or inherited from a base type that the model can see.","If using a string-based overload, switch to the expression-based overload so mismatches surface at compile time."],"exampleFix":"// before\nmodelBuilder.Entity<Order>().HasPartitionKey(\"TenatId\"); // typo\n\n// after\nmodelBuilder.Entity<Order>().HasPartitionKey(o => o.TenantId);","handlingStrategy":"validation","validationCode":"foreach (var et in dbContext.Model.GetEntityTypes())\n{\n    foreach (var name in et.GetPartitionKeyPropertyNames())\n    {\n        if (et.FindProperty(name) is null)\n            throw new InvalidOperationException($\"{et.DisplayName()} partition key property {name} missing\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the lambda overload of HasPartitionKey so name mismatches surface at compile time.","After renaming a partition key property, search for all HasPartitionKey references.","Add a unit test that validates the built model."],"tags":["cosmos","model-validation","partition-key","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}