dotnet/efcore · critical · InvalidOperationException
A call was made to ' ' that changed an option that must be…
Error message
A call was made to '{optionCall}' that changed an option that must be constant within a service provider, but Entity Framework is not building its own internal service provider. Either allow Entity Framework to build the service provider by removing the call to '{useInternalServiceProvider}', or ensure that the configuration for '{optionCall}' does not change for all uses of a given service provider passed to '{useInternalServiceProvider}'. What it means
Thrown by CosmosSingletonOptions.Validate when the CosmosOptionsExtension captured during Initialize() differs from the one on the current context for any of ~17 options (AccountEndpoint, AccountKey, TokenCredential, ConnectionString, Region, PreferredRegions, LimitToEndpoint, ConnectionMode, WebProxy, RequestTimeout, Open/IdleTcpConnectionTimeout, GatewayModeMaxConnectionLimit, MaxTcpConnectionsPerEndpoint, MaxRequestsPerTcpConnection, EnableContentResponseOnWrite, HttpClientFactory, EnableBulkExecution). These are singleton-scoped, so they cannot vary across DbContexts that share one internal service provider.
Solutions
- Stop calling UseInternalServiceProvider and let EF build (and cache) its own provider per option set.
- Or keep the shared provider but make all Cosmos options identical across every DbContext that uses it (same account, region, timeouts, bulk flag, etc.).
- For per-tenant accounts, use a separate internal service provider per tenant (one AddDbContextPool / UseInternalServiceProvider per option set).
Example fix
// before var services = new ServiceCollection(); services.AddDbContext<Actx>(o => o.UseCosmos acctA).UseInternalServiceProvider(sp); services.AddDbContext<Bctx>(o => o.UseCosmos acctB).UseInternalServiceProvider(sp); // after - let EF build providers per option set services.AddDbContext<Actx>(o => o.UseCosmos(acctA, ...)); services.AddDbContext<Bctx>(o => o.UseCosmos(acctB, ...));
Defensive patterns
Strategy: validation
Validate before calling
// Detect divergent Cosmos options across contexts that share a provider.
static IReadOnlyList<CosmosOptionsExtension> CosmosOpts(IServiceProvider sp)
=> sp.GetServices<DbContextOptions>().Select(o => o.FindExtension<CosmosOptionsExtension>()).Where(x => x is not null).ToList()!;
// in startup, after building the provider:
var first = CosmosOpts(sp).FirstOrDefault();
foreach (var ext in CosmosOpts(sp).Skip(1))
if (!Equals(first, ext)) throw new InvalidOperationException("Cosmos singleton options differ; do not share the internal service provider."); Prevention
- Avoid UseInternalServiceProvider unless you can guarantee identical Cosmos options for every DbContext using it.
- For multi-tenant/multi-account scenarios, allocate one internal service provider per option set.
- Keep Cosmos option configuration in a single helper invoked the same way everywhere.
When it happens
Trigger: Calling UseInternalServiceProvider(...) and then registering two or more DbContext types (or two options builders) with different Cosmos options (different endpoints, regions, timeouts, bulk-execution flags, etc.).
Common situations: Multi-tenant apps that point different DbContexts at different Cosmos accounts but pool the service provider; changing connection string/region per request while reusing the pooled provider; tests that swap credentials against a shared provider.
Related errors
- A call was made to ' ' that changed an option that must be…
- 'UseChangeTrackingProxies' and 'UseLazyLoadingProxies' each…
- A full-text index is defined for
- A full-text index on
- A partition key is defined on entity type
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/2903f262a8f5d215.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Cosmos/Infrastructure/Internal/CosmosSingletonOptions.cs:225
|| TokenCredential != cosmosOptions.TokenCredential
|| ConnectionString != cosmosOptions.ConnectionString
|| Region != cosmosOptions.Region
|| !StructuralComparisons.StructuralEqualityComparer.Equals(PreferredRegions, cosmosOptions.PreferredRegions)
|| LimitToEndpoint != cosmosOptions.LimitToEndpoint
|| ConnectionMode != cosmosOptions.ConnectionMode
|| WebProxy != cosmosOptions.WebProxy
|| RequestTimeout != cosmosOptions.RequestTimeout
|| OpenTcpConnectionTimeout != cosmosOptions.OpenTcpConnectionTimeout
|| IdleTcpConnectionTimeout != cosmosOptions.IdleTcpConnectionTimeout
|| GatewayModeMaxConnectionLimit != cosmosOptions.GatewayModeMaxConnectionLimit
|| MaxTcpConnectionsPerEndpoint != cosmosOptions.MaxTcpConnectionsPerEndpoint
|| MaxRequestsPerTcpConnection != cosmosOptions.MaxRequestsPerTcpConnection
|| EnableContentResponseOnWrite != cosmosOptions.EnableContentResponseOnWrite
|| HttpClientFactory != cosmosOptions.HttpClientFactory
|| EnableBulkExecution != cosmosOptions.EnableBulkExecution
))
{
throw new InvalidOperationException(
CoreStrings.SingletonOptionChanged(
nameof(CosmosDbContextOptionsExtensions.UseCosmos),
nameof(DbContextOptionsBuilder.UseInternalServiceProvider)));
}
}
}
View on GitHub (pinned to 3a2006ef56)