{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Infrastructure/Internal/CosmosSingletonOptions.cs#L207-L232","documentation":"Thrown by CosmosSingletonOptions.Validate when a DbContext sharing an internal service provider (set via UseInternalServiceProvider) supplies Cosmos configuration that differs from what was previously cached on the singleton. Validate compares AccountEndpoint, AccountKey, TokenCredential, ConnectionString, Region, PreferredRegions, LimitToEndpoint, ConnectionMode, WebProxy, several timeouts, TCP limits, EnableContentResponseOnWrite, HttpClientFactory, and EnableBulkExecution. Any mismatch on any of those across contexts sharing the provider throws.","triggerScenarios":"Calling DbContextOptionsBuilder.UseInternalServiceProvider(pool) and then registering two contexts whose UseCosmos calls differ (different endpoint, region, connection mode, bulk setting, etc.).","commonSituations":"Using AddDbContextPool or an explicit pooled internal provider with multi-tenant Cosmos where each tenant has a different endpoint/key. Dynamically switching ConnectionMode or Region per request on a pooled context.","solutions":["Make the Cosmos connection options identical for every context that shares the internal service provider (same endpoint, region, mode, bulk, etc.).","If per-tenant options are required, use a separate internal service provider (a separate pool) per distinct Cosmos configuration, or stop calling UseInternalServiceProvider and let EF build its own provider per context.","Centralize UseCosmos configuration in one place and inject the same DbContextOptions instance to all contexts."],"exampleFix":"// before\nservices.AddDbContextPool<TenantAdb>(o => o\n    .UseInternalServiceProvider(sp)\n    .UseCosmos(endpointA, key, \"db\"));\nservices.AddDbContextPool<TenantBdb>(o => o\n    .UseInternalServiceProvider(sp)        // same provider, different endpoint -> throws\n    .UseCosmos(endpointB, key, \"db\"));\n\n// after: give each distinct Cosmos config its own provider\nservices.AddDbContextPool<TenantAdb>(o => o\n    .UseInternalServiceProvider(spA)\n    .UseCosmos(endpointA, key, \"db\"));\nservices.AddDbContextPool<TenantBdb>(o => o\n    .UseInternalServiceProvider(spB)\n    .UseCosmos(endpointB, key, \"db\"));","handlingStrategy":"validation","validationCode":"// Ensure all DbContexts sharing an internal service provider use identical Cosmos options\nvar snapshot = new { Endpoint, Key, Region, ConnectionMode, BulkEnabled } /* extract from your config */;\n// in DI setup, throw if a second context registers different values against the same provider pool","typeGuard":null,"tryCatchPattern":"// At startup, exercise one operation from each pooled context inside a try/catch\ntry { await using var ctx = provider.GetRequiredService<MyDb>(); await ctx.Database.EnsureCreatedAsync(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"must be constant within a service provider\"))\n{ /* log and abort startup: Cosmos options differ across pooled contexts */ }","preventionTips":["Use one internal service provider per distinct Cosmos configuration, or let EF build the provider.","Centralize UseCosmos options in a single configuration method and inject the same DbContextOptions everywhere.","Add a startup smoke test that resolves each context and runs a trivial query to surface this early."],"tags":["cosmos","ef-core","di","service-provider","options"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}