dotnet/efcore · error · ArgumentOutOfRangeException
Specified argument was out of the range of valid values. (Pa
Error message
Specified argument was out of the range of valid values. (Parameter 'connectionMode')
What it means
CosmosOptionsExtension.WithConnectionMode throws ArgumentOutOfRangeException(nameof(connectionMode)) when the supplied Microsoft.Azure.Cosmos.ConnectionMode value is not a defined member of the ConnectionMode enum. The guard uses Enum.IsDefined(typeof(ConnectionMode), connectionMode). It catches garbage produced by an unsafe int->enum cast (e.g. (ConnectionMode)999) before it reaches the Cosmos SDK where the failure would be more obscure.
Source
Thrown at src/EFCore.Cosmos/Infrastructure/Internal/CosmosDbOptionExtension.cs:323
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual ConnectionMode? ConnectionMode
=> _connectionMode;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual CosmosOptionsExtension WithConnectionMode(ConnectionMode connectionMode)
{
if (!Enum.IsDefined(typeof(ConnectionMode), connectionMode))
{
throw new ArgumentOutOfRangeException(nameof(connectionMode));
}
var clone = Clone();
clone._connectionMode = connectionMode;
return clone;
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual IWebProxy? WebProxy
=> _webProxy;
View on GitHub (pinned to 3a2006ef56)
Solutions
- Validate the value with Enum.IsDefined(typeof(ConnectionMode), value) before casting; fall back to Gateway or Direct if it is not defined.
- Bind configuration as a named string and parse with Enum.TryParse<ConnectionMode>(name, out var mode).
- Use the strongly typed ConnectionMode member (ConnectionMode.Direct or ConnectionMode.Gateway) at the call site rather than integer casts.
- If you are not sure which modes are valid for your SDK version, enumerate Enum.GetNames<ConnectionMode>() and log them at startup.
Example fix
// before - throws for any int that is not Gateway(0)/Direct(1)/Gateway(1) per SDK
var mode = (ConnectionMode)configuration.GetValue<int>("Cosmos:ConnectionMode");
options.WithConnectionMode(mode);
// after - parse by name with a safe default
var name = configuration.GetValue<string>("Cosmos:ConnectionMode");
var mode = Enum.TryParse<ConnectionMode>(name, ignoreCase: true, out var m)
? m
: ConnectionMode.Gateway;
options.WithConnectionMode(mode); Defensive patterns
Strategy: validation
Validate before calling
// Validate before passing to WithConnectionMode.
static ConnectionMode SafeConnectionMode(int raw, ConnectionMode fallback = ConnectionMode.Gateway)
=> Enum.IsDefined(typeof(ConnectionMode), raw) ? (ConnectionMode)raw : fallback; Type guard
static bool IsValid(ConnectionMode mode) => Enum.IsDefined(typeof(ConnectionMode), mode);
Prevention
- Bind ConnectionMode from configuration by name (Enum.TryParse), not by int cast.
- Use the typed member (ConnectionMode.Direct / Gateway) at call sites where possible.
- After upgrading the Azure Cosmos SDK, re-check the defined ConnectionMode members.
- Wrap every int->ConnectionMode cast in a SafeConnectionMode helper.
When it happens
Trigger: Casting an arbitrary integer to ConnectionMode and passing it: options.WithConnectionMode((ConnectionMode)cfg.Value); reading the mode from configuration as int and casting without validation; downgrading the Azure Cosmos SDK where a ConnectionMode member was removed so a serialized value no longer maps to a defined member.
Common situations: Loading ConnectionMode from a settings file as int and casting directly; reflection-based configuration that bypasses compile-time enum checks; SDK version skew between the configured value and the loaded Azure.Cosmos assembly.
Related errors
- The value '{value}' provided for argument '{argumentName}' m
- The value '{value}' provided for argument '{argumentName}' m
- Cosmos-specific methods can only be used when the context is
- The partition key value is of type '{valueType}' which is no
- The partition key value supplied for '{propertyType}' proper
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/da2f1c9b047ab447.
Report an issue: GitHub.