dotnet/efcore · error · InvalidOperationException

The partition key for entity type '{entityType}' is set to '

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 by ValidateKeys when a name returned by GetPartitionKeyPropertyNames() does not resolve to any property via entityType.FindProperty(name). The partition-key annotation lists property names that must exist on the entity; a dangling name means HasPartitionKey referenced a property that was never declared or was removed.

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 dbf9771522)

Solutions

  1. Use the lambda overload HasPartitionKey(e => e.RegionId) so the compiler verifies the property exists.
  2. If using the string overload, ensure the name exactly matches a declared property (case-sensitive).
  3. Declare the property before configuring it as the partition key.

Example fix

// before
modelBuilder.Entity<Order>().HasPartitionKey("RegionId"); // property is Region

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

Strategy: validation

Validate before calling

using var ctx = new MyContext();
foreach (var e in ctx.Model.GetEntityTypes())
{
    foreach (var name in e.GetPartitionKeyPropertyNames())
    {
        Debug.Assert(e.FindProperty(name) is not null, $"{e.Name}: partition key property '{name}' does not exist");
    }
}

Try / catch

try { ctx.Model.GetModel(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("there is no property with that name"))
{
    logger.LogError(ex, "Partition key references a non-existent property");
    throw;
}

Prevention

When it happens

Trigger: Calling HasPartitionKey("RegionId") (string overload) when no property named RegionId exists, or setting partition key property names directly via SetPartitionKeyPropertyNames with a typo.

Common situations: Renaming a property but not the partition-key reference; using the string-based overload with a mismatched casing/spelling; setting the partition key before defining the property.

Related errors


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