{"record":{"id":"bc76b2d9ded00a7e","repo":"dotnet/efcore","slug":"the-partition-key-properties-for-entity-type-ent","errorCode":null,"errorMessage":"The partition key properties for entity type '{entityType1}' are '{props1}', while the partition key properties for entity type '{entityType2}' are '{props2}', and both entity types are mapped to the container '{containerName}'. All entity types mapped to the same container must have compatible partition keys defined.","messagePattern":"The partition key properties for entity type '(.+?)' are '(.+?)', while the partition key properties for entity type '(.+?)' are '(.+?)', and both entity types are mapped to the container '(.+?)'\\. All entity types mapped to the same container must have compatible partition keys defined\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs","lineNumber":166,"sourceCode":"\n        foreach (var entityType in mappedTypes)\n        {\n            Check.DebugAssert(entityType.IsDocumentRoot(), \"Only document roots expected here.\");\n\n            var storeNames = entityType.GetPartitionKeyPropertyNames()\n                .Select(n => entityType.FindProperty(n)?.GetJsonPropertyName())\n                .ToList();\n\n            if (firstEntityType is null)\n            {\n                partitionKeyStoreNames = storeNames;\n                firstEntityType = entityType;\n            }\n            else\n            {\n                if (partitionKeyStoreNames.Count != storeNames.Count)\n                {\n                    throw new InvalidOperationException(\n                        CosmosStrings.NoPartitionKey(\n                            firstEntityType.DisplayName(),\n                            string.Join(\",\", partitionKeyStoreNames),\n                            entityType.DisplayName(),\n                            string.Join(\",\", storeNames),\n                            container));\n                }\n\n                for (var i = 0; i < storeNames.Count; i++)\n                {\n                    if (!string.Equals(storeNames[i], partitionKeyStoreNames[i], StringComparison.Ordinal))\n                    {\n                        throw new InvalidOperationException(\n                            CosmosStrings.PartitionKeyStoreNameMismatch(\n                                firstEntityType.GetPartitionKeyPropertyNames()[i],\n                                firstEntityType.DisplayName(),\n                                partitionKeyStoreNames[i],\n                                entityType.GetPartitionKeyPropertyNames()[i],","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L148-L184","documentation":"CosmosModelValidator.ValidateSharedContainerCompatibility throws InvalidOperationException(CosmosStrings.NoPartitionKey) when two entity types mapped to the same container have partition key property sets of different counts. Cosmos requires every entity type sharing a container to declare the same partition key shape (same number of properties). The check fails when partitionKeyStoreNames.Count != storeNames.Count for a subsequent entity type in the same container.","triggerScenarios":"EntityA declares HasPartitionKey(a => a.TenantId) and EntityB (same container) declares no partition key or two; sharing a container between entity types via HasContainerName without aligning partition keys; one entity in a discriminator-shared container has a composite partition key while others have a single key.","commonSituations":"Sharing containers between unrelated entity types for cost/throughput reasons without normalizing partition keys; partial configuration where some types set partition keys and others rely on defaults; discriminator-based multi-type containers where partition key was configured on only some types.","solutions":["Align the partition key declaration on every entity type sharing the container: same number of properties, same order.","If the entity types genuinely need different partition keys, map them to separate containers.","Centralize partition key configuration in a shared convention or IEntityTypeConfiguration so all types in a container receive the same shape.","After changing partition key configuration, re-run model validation at startup (EnsureCreated or a no-op query) to catch the mismatch early."],"exampleFix":"// before - throws: same container, different partition key counts\nmodelBuilder.Entity<A>().HasPartitionKey(a => a.TenantId).ToContainer(\"Shared\");\nmodelBuilder.Entity<B>().ToContainer(\"Shared\"); // no partition key\n\n// after - align partition keys across the shared container\nmodelBuilder.Entity<A>().HasPartitionKey(a => a.TenantId).ToContainer(\"Shared\");\nmodelBuilder.Entity<B>().HasPartitionKey(b => b.TenantId).ToContainer(\"Shared\");","handlingStrategy":"validation","validationCode":"// Align partition keys before model validation.\nstatic void AlignPartitionKeys(ModelBuilder mb, string container, params string[] props)\n{\n    foreach (var et in mb.Model.GetEntityTypes().Where(e => e.GetContainer() == container))\n    {\n        // re-declare the same partition key shape on every type\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Share partition key configuration across all entity types in the same container.","If types need different partition keys, move them to separate containers.","Centralize partition key setup in a shared IEntityTypeConfiguration or convention.","Run model validation at startup to catch shape mismatches early."],"tags":["ef-core","cosmos","model-validation","partition-key","shared-container"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}