dotnet/efcore · error · InvalidOperationException
The partition key properties for entity type '{entityType1}'
Error message
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. What it means
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.
Source
Thrown at src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs:166
foreach (var entityType in mappedTypes)
{
Check.DebugAssert(entityType.IsDocumentRoot(), "Only document roots expected here.");
var storeNames = entityType.GetPartitionKeyPropertyNames()
.Select(n => entityType.FindProperty(n)?.GetJsonPropertyName())
.ToList();
if (firstEntityType is null)
{
partitionKeyStoreNames = storeNames;
firstEntityType = entityType;
}
else
{
if (partitionKeyStoreNames.Count != storeNames.Count)
{
throw new InvalidOperationException(
CosmosStrings.NoPartitionKey(
firstEntityType.DisplayName(),
string.Join(",", partitionKeyStoreNames),
entityType.DisplayName(),
string.Join(",", storeNames),
container));
}
for (var i = 0; i < storeNames.Count; i++)
{
if (!string.Equals(storeNames[i], partitionKeyStoreNames[i], StringComparison.Ordinal))
{
throw new InvalidOperationException(
CosmosStrings.PartitionKeyStoreNameMismatch(
firstEntityType.GetPartitionKeyPropertyNames()[i],
firstEntityType.DisplayName(),
partitionKeyStoreNames[i],
entityType.GetPartitionKeyPropertyNames()[i],View on GitHub (pinned to dbf9771522)
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.
Example fix
// before (mismatched partition key counts in one container)
modelBuilder.Entity<Order>().ToContainer("docs").HasPartitionKey(o => o.TenantId);
modelBuilder.Entity<Invoice>().ToContainer("docs").HasPartitionKey(i => i.Region).HasPartitionKey(i => i.CustomerId);
// after (both use the same single partition key)
modelBuilder.Entity<Order>().ToContainer("docs").HasPartitionKey(o => o.TenantId);
modelBuilder.Entity<Invoice>().ToContainer("docs").HasPartitionKey(i => i.TenantId); Defensive patterns
Strategy: validation
Validate before calling
// All roots in one container must have the same partition key property count.
var byContainer = modelBuilder.Model.GetEntityTypes()
.Where(e => e.FindPrimaryKey() != null && e.GetContainer() != null)
.GroupBy(e => e.GetContainer());
foreach (var g in byContainer)
{
var counts = g.Select(e => e.GetPartitionKeyPropertyNames().Count).Distinct();
if (counts.Count() > 1)
throw new InvalidOperationException($"Container {g.Key} has mismatched partition key counts.");
} Try / catch
try { context.Database.EnsureCreated(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("compatible partition keys"))
{ throw new InvalidOperationException("Align partition key property sets across types in the shared container.", ex); } Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- The partition key property '{property1}' on '{entityType1}'
- The entity type '{entityType}' is sharing the container '{co
- The entity type '{entityType}' is sharing the container '{co
- The discriminator value for '{entityType1}' is '{discriminat
- The IsDiscriminatorMappingComplete setting was configured to
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/bc76b2d9ded00a7e.
Report an issue: GitHub.