dotnet/efcore · error · InvalidOperationException

The partition key for entity type

Error message

The partition key for entity type '{entityType}' is set to '{property}', but there is no property with that name.

What it means

Thrown when the partition key references a property name that does not exist on the entity type. The validator iterates the partition key property names and calls entityType.FindProperty(name); a null result means no property is declared with that name, so the partition key cannot be honored.

Solutions

  1. Verify the property name in the HasPartitionKey lambda matches a real property on the root entity (use the lambda form, not a magic string, to get compile-time safety).
  2. If the property was renamed, update the partition-key mapping to the new name.
  3. Ensure the property is declared on the root entity type or inherited from a base type that the model can see.
  4. If using a string-based overload, switch to the expression-based overload so mismatches surface at compile time.

Example fix

// before
modelBuilder.Entity<Order>().HasPartitionKey("TenatId"); // typo

// after
modelBuilder.Entity<Order>().HasPartitionKey(o => o.TenantId);
Defensive patterns

Strategy: validation

Validate before calling

foreach (var et in dbContext.Model.GetEntityTypes())
{
    foreach (var name in et.GetPartitionKeyPropertyNames())
    {
        if (et.FindProperty(name) is null)
            throw new InvalidOperationException($"{et.DisplayName()} partition key property {name} missing");
    }
}

Prevention

When it happens

Trigger: Calling HasPartitionKey(e => e.SomeField) where SomeField is misspelled, was renamed, or never declared on the entity (or its base types). Also triggered by string-based HasPartitionKey("WrongName"). Fires during ValidateKeys.

Common situations: Renaming a property in code but forgetting to update the partition-key mapping; using the JSON property name instead of the CLR property name; copying configuration between entities that have differently named fields; typos in the lambda or string.

Related errors


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

Appendix: source

Thrown at src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs:467

        if (partitionKeyPropertyNames.Count == 0)
        {
            logger.NoPartitionKeyDefined(entityType);
        }
        else
        {
            if (entityType.BaseType != null
                && entityType.FindAnnotation(CosmosAnnotationNames.PartitionKeyNames)?.Value != null)
            {
                throw new InvalidOperationException(
                    CosmosStrings.PartitionKeyNotOnRoot(entityType.DisplayName(), entityType.BaseType.DisplayName()));
            }

            foreach (var partitionKeyPropertyName in partitionKeyPropertyNames)
            {
                var partitionKey = entityType.FindProperty(partitionKeyPropertyName);
                if (partitionKey == null)
                {
                    throw new InvalidOperationException(
                        CosmosStrings.PartitionKeyMissingProperty(entityType.DisplayName(), partitionKeyPropertyName));
                }

                var partitionKeyType = (partitionKey.GetTypeMapping().Converter?.ProviderClrType
                    ?? partitionKey.ClrType).UnwrapNullableType();
                if (partitionKeyType != typeof(string)
                    && !partitionKeyType.IsNumeric()
                    && partitionKeyType != typeof(bool))
                {
                    throw new InvalidOperationException(
                        CosmosStrings.PartitionKeyBadStoreType(
                            partitionKeyPropertyName,
                            entityType.DisplayName(),
                            partitionKeyType.ShortDisplayName()));
                }
            }
        }
    }

View on GitHub (pinned to 3a2006ef56)