dotnet/efcore · error · InvalidOperationException

The type of the partition key property '{property}' on '{ent

Error message

The type of the partition key property '{property}' on '{entityType}' is '{propertyType}'. All partition key property types must be numeric, Boolean, or string, or converted to one of these types.

What it means

Thrown by ValidateKeys when the partition-key property's effective type (ClrType after any value converter, unwrapped from Nullable<T>) is not string, a numeric type, or bool. Cosmos partition keys support only those primitive types, so EF rejects unsupported partition-key property types at validation.

Source

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

                    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()));
                }
            }
        }
    }

    /// <summary>
    ///     Validates that a key doesn't have mutable properties.
    /// </summary>
    /// <param name="key">The key to validate.</param>
    /// <param name="logger">The logger to use.</param>
    protected override void ValidateMutableKey(
        IKey key,
        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
    {

View on GitHub (pinned to dbf9771522)

Solutions

  1. Choose a string, numeric, or bool property as the partition key.
  2. Add a value converter to a supported type, e.g. .HasConversion(v => v.ToString(), v => Guid.Parse(v)) for a Guid partition key.
  3. Add a computed/derived partition-key property of a supported type and partition on that.

Example fix

// before
modelBuilder.Entity<Event>().HasPartitionKey(e => e.Timestamp); // DateTime -> rejected

// after
modelBuilder.Entity<Event>()
    .Property(e => e.DayKey).HasConversion<int>();
modelBuilder.Entity<Event>().HasPartitionKey(e => e.DayKey);
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())
    {
        var p = e.FindProperty(name);
        if (p is null) continue;
        var t = (p.GetTypeMapping().Converter?.ProviderClrType ?? p.ClrType).UnwrapNullableType();
        Debug.Assert(t == typeof(string) || t == typeof(bool) || t.IsNumeric(),
            $"{e.Name}.{name} partition key type {t} is not numeric/bool/string");
    }
}

Try / catch

try { ctx.Model.GetModel(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("partition key property types must be numeric"))
{
    logger.LogError(ex, "Partition key property has an unsupported type");
    throw;
}

Prevention

When it happens

Trigger: Configuring a Guid, DateTime, enum, or complex-typed property as the partition key without converting it to a numeric/string/bool provider type.

Common situations: Using a natural-key Guid or DateTime as the partition key; enums as partition keys; forgetting that the property type must match Cosmos's supported set.

Related errors


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