dotnet/efcore · error · InvalidOperationException
The partition key property '{property1}' on '{entityType1}'
Error message
The partition key property '{property1}' on '{entityType1}' is mapped as '{storeName1}', but the partition key property '{property2}' on '{entityType2}' is mapped as '{storeName2}'. All partition key properties need to be mapped to the same store property for entity types mapped to the same container. What it means
When the partition key property counts match but their JSON store names (ToJsonProperty) differ between entity types sharing a container, CosmosModelValidator throws InvalidOperationException via CosmosStrings.PartitionKeyStoreNameMismatch. Cosmos needs one physical partition key path, so every type must map its partition key to the same JSON property name.
Source
Thrown at src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs:179
}
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],
entityType.DisplayName(),
storeNames[i]));
}
}
}
if (mappedTypes.Count == 1)
{
break;
}
if (entityType.ClrType.IsInstantiable()
&& entityType.GetContainingPropertyName() == null)View on GitHub (pinned to dbf9771522)
Solutions
- Map the partition key property to the same JSON property name (ToJsonProperty) across all entity types in the shared container.
- Remove conflicting ToJsonProperty renames so each type's partition key resolves to the same store path.
- If names truly must differ, the types belong in separate containers.
Example fix
// before (partition key JSON names diverge in one container)
modelBuilder.Entity<Order>().ToContainer("docs").HasPartitionKey(o => o.TenantId)
.Property(o => o.TenantId).ToJsonProperty("tid");
modelBuilder.Entity<Invoice>().ToContainer("docs").HasPartitionKey(i => i.TenantId)
.Property(i => i.TenantId).ToJsonProperty("tenant_id");
// after (same JSON name for the partition key)
modelBuilder.Entity<Order>().ToContainer("docs").HasPartitionKey(o => o.TenantId)
.Property(o => o.TenantId).ToJsonProperty("tenantId");
modelBuilder.Entity<Invoice>().ToContainer("docs").HasPartitionKey(i => i.TenantId)
.Property(i => i.TenantId).ToJsonProperty("tenantId"); Defensive patterns
Strategy: validation
Validate before calling
// Partition key JSON store names must match across types in a container.
var byContainer = modelBuilder.Model.GetEntityTypes()
.Where(e => e.FindPrimaryKey() != null && e.GetContainer() != null)
.GroupBy(e => e.GetContainer());
foreach (var g in byContainer)
{
var storeNames = g.Select(e => string.Join(",",
e.GetPartitionKeyPropertyNames().Select(n => e.FindProperty(n)?.GetJsonPropertyName()))).Distinct();
if (storeNames.Count() > 1)
throw new InvalidOperationException($"Container {g.Key} has mismatched partition key JSON names.");
} Try / catch
try { context.Database.EnsureCreated(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("same store property"))
{ throw new InvalidOperationException("Map partition key properties to the same JSON name in the shared container.", ex); } Prevention
- Use the same ToJsonProperty name for partition key properties across a shared container.
- Avoid renaming only one type's partition key JSON property.
- Validate store-name consistency in a model convention.
When it happens
Trigger: Two types share a container and both declare a partition key property, but one renames the partition key's JSON property via ToJsonProperty to a different name than the other.
Common situations: Renaming the partition key JSON property on one type only; legacy property names that differ across roots forced into one container; copy-paste of ToJsonProperty that diverges.
Related errors
- The partition key properties for entity type '{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/dc57c0c82c33eb8f.
Report an issue: GitHub.