dotnet/efcore · error · InvalidOperationException

The partition key property

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

CosmosModelValidator.ValidateSharedContainerCompatibility throws InvalidOperationException(CosmosStrings.PartitionKeyStoreNameMismatch) when two entity types in the same container declare partition keys with the same count but their JSON store names (the value returned by GetJsonPropertyName for each partition key property) differ at the same position. Cosmos requires every type in a shared container to map its partition key properties to the same JSON property name(s) so the server-side partition function is consistent.

Solutions

  1. Align the JSON property name of each partition key property across all entity types in the shared container using ToJsonProperty/HasJsonPropertyName.
  2. Use the same C# property name (and therefore the same default JSON name) for the partition key across all types.
  3. If the names genuinely differ, move the types into separate containers.
  4. Validate the model at startup so the mismatch surfaces before the first query.

Example fix

// before - throws: same container, JSON names differ at position 0
modelBuilder.Entity<A>()
    .HasPartitionKey(a => a.TenantId)   // -> JSON "tenantId"
    .ToContainer("Shared");
modelBuilder.Entity<B>()
    .HasPartitionKey(b => b.TenantKey)  // -> JSON "tenantKey"
    .ToContainer("Shared");

// after - align the JSON name on B's partition key
modelBuilder.Entity<B>()
    .HasPartitionKey(b => b.TenantKey)
    .ToContainer("Shared");
modelBuilder.Entity<B>()
    .Property(b => b.TenantKey).ToJsonProperty("tenantId");
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the JSON property names of partition key properties line up.
static bool PartitionKeyJsonNamesAligned(IReadOnlyList<IEntityType> types)
{
    var reference = types[0].GetPartitionKeyPropertyNames()
        .Select(n => types[0].FindProperty(n)!.GetJsonPropertyName()).ToList();
    return types.Skip(1).All(t => t.GetPartitionKeyPropertyNames()
        .Select(n => t.FindProperty(n)!.GetJsonPropertyName()).SequenceEqual(reference));
}

Prevention

When it happens

Trigger: EntityA.HasPartitionKey(a => a.TenantId) maps TenantId to JSON "tenantId", but EntityB.HasPartitionKey(b => b.TenantKey) (same container) maps TenantKey to JSON "tenantKey"; renaming a partition key property without renaming its JSON mapping; using ToJsonProperty on one partition key property but not the corresponding one on a sibling entity type.

Common situations: Sharing a container between entity types that named the partition key differently in C# but forgot to align the JSON name; refactor/rename of a partition key property on one type only; types added later to an existing shared container without aligning JSON names.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/dc57c0c82c33eb8f. Report an issue: GitHub.

Appendix: 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 3a2006ef56)