dotnet/efcore · error · InvalidOperationException
The partition key value supplied for '{propertyType}' proper
Error message
The partition key value supplied for '{propertyType}' property '{entityType}.{property}' is of type '{valueType}'. Partition key values must be of a type assignable to the property. What it means
After value conversion, PartitionKeyBuilderExtensions.Add checks that the runtime value's CLR type matches the partition key property's expected type via the local CheckType function; a mismatch throws InvalidOperationException via CosmosStrings.PartitionKeyBadValueType. E.g. passing a string value for a partition key property typed int.
Source
Thrown at src/EFCore.Cosmos/Extensions/Internal/PartitionKeyBuilderExtensions.cs:85
case var _ when value.GetType().IsNumeric():
if (expectedType != null && !expectedType.IsNumeric())
{
CheckType(value.GetType());
}
builder.Add(Convert.ToDouble(value));
break;
default:
throw new InvalidOperationException(CosmosStrings.PartitionKeyBadValue(value.GetType()));
}
void CheckType(Type actualType)
{
if (expectedType != null && expectedType != actualType)
{
throw new InvalidOperationException(
CosmosStrings.PartitionKeyBadValueType(
expectedType.ShortDisplayName(),
property!.DeclaringType.DisplayName(),
property.Name,
actualType.DisplayName()));
}
}
}
return builder;
}
}
View on GitHub (pinned to dbf9771522)
Solutions
- Pass a value whose type matches the partition key property's CLR (or provider) type exactly.
- Re-check the property type declared via HasPartitionKey and align the WithPartitionKey argument to it.
- If a converter is in play, pass a value of the converter's ProviderClrType, not the original CLR type.
Example fix
// before (string passed for an int partition key property)
var q = context.Items.WithPartitionKey("123");
// after (int matches the declared partition key property)
var q = context.Items.WithPartitionKey(123); Defensive patterns
Strategy: validation
Validate before calling
// Ensure the passed value matches the partition key property's CLR type.
var pkProp = context.Model.FindEntityType(typeof(Item))!
.GetPartitionKeyPropertyNames().First();
var expected = context.Model.FindEntityType(typeof(Item))!.FindProperty(pkProp)!.ClrType;
if (value != null && !expected.IsAssignableFrom(value.GetType()))
throw new ArgumentException($"Partition key value must be {expected.Name}.");
return context.Items.WithPartitionKey(value); Type guard
static bool MatchesPartitionType(IModel model, Type entity, object? value)
{
var pk = model.FindEntityType(entity)!.GetPartitionKeyPropertyNames().FirstOrDefault();
if (pk is null) return true;
var t = model.FindEntityType(entity)!.FindProperty(pk)!.ClrType;
return value is null || t.IsAssignableFrom(value.GetType());
} Try / catch
try { return context.Items.WithPartitionKey(value).ToList(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Partition key values must be"))
{ throw new ArgumentException($"Pass a value of the partition key property's type.", ex); } Prevention
- Pass a value whose type matches the declared partition key property.
- After changing the property type, update all WithPartitionKey callers.
- When a value converter is present, pass the provider-side type, not the CLR type.
When it happens
Trigger: Calling WithPartitionKey with a value whose type differs from the declared partition key property type (e.g. WithPartitionKey("123") when the property is int); a converter that changes the provider type such that the runtime value no longer matches.
Common situations: Loosely typed WithPartitionKey(object) call with a boxed value of the wrong type; refactoring the partition key property type without updating callers; a value converter that yields a different provider type.
Related errors
- The partition key value is of type '{valueType}' which is no
- The type '{givenType}' cannot be mapped as a dictionary beca
- The partition key properties for entity type '{entityType1}'
- The partition key property '{property1}' on '{entityType1}'
- A partition key is defined on entity type '{entityType}', wh
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/723c191c9b1b5d36.
Report an issue: GitHub.