{"record":{"id":"2903f262a8f5d215","repo":"dotnet/efcore","slug":"a-call-was-made-to-optioncall-that-changed-an","errorCode":null,"errorMessage":"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}'.","messagePattern":"A call was made to '(.+?)' 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 '(.+?)', or ensure that the configuration for '(.+?)' does not change for all uses of a given service provider passed to '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/EFCore.Cosmos/Infrastructure/Internal/CosmosSingletonOptions.cs","lineNumber":225,"sourceCode":"                || TokenCredential != cosmosOptions.TokenCredential\n                || ConnectionString != cosmosOptions.ConnectionString\n                || Region != cosmosOptions.Region\n                || !StructuralComparisons.StructuralEqualityComparer.Equals(PreferredRegions, cosmosOptions.PreferredRegions)\n                || LimitToEndpoint != cosmosOptions.LimitToEndpoint\n                || ConnectionMode != cosmosOptions.ConnectionMode\n                || WebProxy != cosmosOptions.WebProxy\n                || RequestTimeout != cosmosOptions.RequestTimeout\n                || OpenTcpConnectionTimeout != cosmosOptions.OpenTcpConnectionTimeout\n                || IdleTcpConnectionTimeout != cosmosOptions.IdleTcpConnectionTimeout\n                || GatewayModeMaxConnectionLimit != cosmosOptions.GatewayModeMaxConnectionLimit\n                || MaxTcpConnectionsPerEndpoint != cosmosOptions.MaxTcpConnectionsPerEndpoint\n                || MaxRequestsPerTcpConnection != cosmosOptions.MaxRequestsPerTcpConnection\n                || EnableContentResponseOnWrite != cosmosOptions.EnableContentResponseOnWrite\n                || HttpClientFactory != cosmosOptions.HttpClientFactory\n                || EnableBulkExecution != cosmosOptions.EnableBulkExecution\n            ))\n        {\n            throw new InvalidOperationException(\n                CoreStrings.SingletonOptionChanged(\n                    nameof(CosmosDbContextOptionsExtensions.UseCosmos),\n                    nameof(DbContextOptionsBuilder.UseInternalServiceProvider)));\n        }\n    }\n}\n","sourceCodeStart":207,"sourceCodeEnd":232,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Infrastructure/Internal/CosmosSingletonOptions.cs#L207-L232","documentation":"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.","triggerScenarios":"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.).","commonSituations":"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.","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)."],"exampleFix":"// before\nvar services = new ServiceCollection();\nservices.AddDbContext<Actx>(o => o.UseCosmos acctA).UseInternalServiceProvider(sp);\nservices.AddDbContext<Bctx>(o => o.UseCosmos acctB).UseInternalServiceProvider(sp);\n\n// after - let EF build providers per option set\nservices.AddDbContext<Actx>(o => o.UseCosmos(acctA, ...));\nservices.AddDbContext<Bctx>(o => o.UseCosmos(acctB, ...));","handlingStrategy":"validation","validationCode":"// Detect divergent Cosmos options across contexts that share a provider.\nstatic IReadOnlyList<CosmosOptionsExtension> CosmosOpts(IServiceProvider sp)\n    => sp.GetServices<DbContextOptions>().Select(o => o.FindExtension<CosmosOptionsExtension>()).Where(x => x is not null).ToList()!;\n// in startup, after building the provider:\nvar first = CosmosOpts(sp).FirstOrDefault();\nforeach (var ext in CosmosOpts(sp).Skip(1))\n    if (!Equals(first, ext)) throw new InvalidOperationException(\"Cosmos singleton options differ; do not share the internal service provider.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["cosmos","di","singleton-options","configuration","service-provider"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}