{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L148-L184","documentation":"All entity types sharing a Cosmos container must have compatible partition keys (same number of partition key properties). CosmosModelValidator throws InvalidOperationException via CosmosStrings.NoPartitionKey when the partition key property-name counts differ between two types in the same container, since Cosmos uses a single physical partition key path per container.","triggerScenarios":"Two entity types mapped to the same container where one has HasPartitionKey configured (1+ properties) and the other has none, or they specify different numbers of partition key properties (e.g. single-key vs hierarchical).","commonSituations":"Sharing a container across multiple roots and forgetting to set partition keys on all of them; mixing single and hierarchical partition keys in one container; copy-paste of entity configs into a shared container.","solutions":["Give every entity type mapped to the shared container the same partition key property set (same count and compatible names).","If a type needs a different partition key strategy, map it to a different container.","Centralize partition-key configuration in a shared convention so all roots in a container agree."],"exampleFix":"// before (mismatched partition key counts in one container)\nmodelBuilder.Entity<Order>().ToContainer(\"docs\").HasPartitionKey(o => o.TenantId);\nmodelBuilder.Entity<Invoice>().ToContainer(\"docs\").HasPartitionKey(i => i.Region).HasPartitionKey(i => i.CustomerId);\n\n// after (both use the same single partition key)\nmodelBuilder.Entity<Order>().ToContainer(\"docs\").HasPartitionKey(o => o.TenantId);\nmodelBuilder.Entity<Invoice>().ToContainer(\"docs\").HasPartitionKey(i => i.TenantId);","handlingStrategy":"validation","validationCode":"// All roots in one container must have the same partition key property count.\nvar byContainer = modelBuilder.Model.GetEntityTypes()\n    .Where(e => e.FindPrimaryKey() != null && e.GetContainer() != null)\n    .GroupBy(e => e.GetContainer());\nforeach (var g in byContainer)\n{\n    var counts = g.Select(e => e.GetPartitionKeyPropertyNames().Count).Distinct();\n    if (counts.Count() > 1)\n        throw new InvalidOperationException($\"Container {g.Key} has mismatched partition key counts.\");\n}","typeGuard":null,"tryCatchPattern":"try { context.Database.EnsureCreated(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"compatible partition keys\"))\n{ throw new InvalidOperationException(\"Align partition key property sets across types in the shared container.\", ex); }","preventionTips":["Give every type in a shared container the same partition key property set.","Centralize partition-key config in a shared convention per container.","Move types needing a different partition key into separate containers."],"tags":["cosmos","partition-key","shared-container","model-validation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}