{"record":{"id":"431166fd165f28d9","repo":"dotnet/efcore","slug":"a-call-was-made-to-optioncall-that-changed-an-431166","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":"error","filePath":"src/EFCore.InMemory/Infrastructure/Internal/InMemorySingletonOptions.cs","lineNumber":43,"sourceCode":"        {\n            IsNullabilityCheckEnabled = inMemoryOptions.IsNullabilityCheckEnabled;\n        }\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual void Validate(IDbContextOptions options)\n    {\n        var inMemoryOptions = options.FindExtension<InMemoryOptionsExtension>();\n\n        if (inMemoryOptions != null\n            && IsNullabilityCheckEnabled != inMemoryOptions.IsNullabilityCheckEnabled)\n        {\n            throw new InvalidOperationException(\n                CoreStrings.SingletonOptionChanged(\n                    nameof(InMemoryDbContextOptionsBuilder.EnableNullChecks),\n                    nameof(DbContextOptionsBuilder.UseInternalServiceProvider)));\n        }\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual bool IsNullabilityCheckEnabled { get; private set; }\n}\n","sourceCodeStart":25,"sourceCodeEnd":58,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.InMemory/Infrastructure/Internal/InMemorySingletonOptions.cs#L25-L58","documentation":"Thrown by InMemorySingletonOptions.Validate when the configured IsNullabilityCheckEnabled value differs between DbContext instances sharing the same internal (singleton) service provider. EF Core caches singleton options per internal service provider, so any per-context variation in this option is rejected to prevent silent cross-context corruption.","triggerScenarios":"Using DbContextOptionsBuilder.UseInternalServiceProvider(sp) and configuring one context with EnableNullChecks (or the IsNullabilityCheckEnabled option) while another context on the same provider does not, then Validate is invoked.","commonSituations":"Pooling or manually sharing an internal service provider across contexts whose nullability-check preference differs. Toggling EnableNullChecks in tests while the production context already created the singleton options. Disposing/re-creating contexts against a shared provider with different settings.","solutions":["Make EnableNullChecks consistent across every DbContext that shares the internal service provider.","Remove UseInternalServiceProvider and let EF build (and cache) its own provider per options configuration.","Use distinct internal service providers for contexts that must differ in null-check behavior.","Set the option once at startup via a shared configuration helper so all contexts agree."],"exampleFix":"// before - inconsistent across shared provider\noptionsA.UseInternalServiceProvider(sp).UseInMemoryDatabase(\"A\");\noptionsB.UseInMemoryDatabase(\"B\").EnableNullChecks();\n// after - agree on the option\noptionsA.UseInMemoryDatabase(\"A\").EnableNullChecks();\noptionsB.UseInMemoryDatabase(\"B\").EnableNullChecks();","handlingStrategy":"validation","validationCode":"// Ensure all contexts sharing the provider agree on EnableNullChecks\nbool want = config.GetValue<bool>(\"InMemory:EnableNullChecks\");\noptions.UseInMemoryDatabase(\"db\");\nif (want) ((InMemoryDbContextOptionsBuilder)options.Extensions...).EnableNullChecks();\n// simpler: centralize options construction","typeGuard":null,"tryCatchPattern":"try { dbContext.Database.EnsureCreated(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"SingletonOptionChanged\"))\n{\n    // reconcile the option across contexts or drop UseInternalServiceProvider\n}","preventionTips":["Construct DbContext options once via a factory so every shared-provider context agrees.","Avoid UseInternalServiceProvider unless you pool contexts with identical options.","Store the nullability-check setting in a single config value read everywhere."],"tags":["in-memory","configuration","singleton-options","service-provider","validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}