{"record":{"id":"d3a93654e36fff31","repo":"dotnet/efcore","slug":"multiple-relational-database-provider-configuratio","errorCode":null,"errorMessage":"Multiple relational database provider configurations found. A context can only be configured to use a single database provider.","messagePattern":"Multiple relational database provider configurations found\\. A context can only be configured to use a single database provider\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/EFCore.Relational/Infrastructure/RelationalOptionsExtension.cs","lineNumber":424,"sourceCode":"\n    /// <summary>\n    ///     Finds an existing <see cref=\"RelationalOptionsExtension\" /> registered on the given options\n    ///     or throws if none has been registered. This is typically used to find some relational\n    ///     configuration when it is known that a relational provider is being used.\n    /// </summary>\n    /// <param name=\"options\">The context options to look in.</param>\n    /// <returns>The extension.</returns>\n    public static RelationalOptionsExtension Extract(IDbContextOptions options)\n    {\n        var relationalOptionsExtensions\n            = options.Extensions\n                .OfType<RelationalOptionsExtension>()\n                .ToList();\n\n        return relationalOptionsExtensions.Count == 0\n            ? throw new InvalidOperationException(RelationalStrings.NoProviderConfigured)\n            : relationalOptionsExtensions.Count > 1\n                ? throw new InvalidOperationException(RelationalStrings.MultipleProvidersConfigured)\n                : relationalOptionsExtensions[0];\n    }\n\n    /// <summary>\n    ///     Adds the services required to make the selected options work. This is used when there\n    ///     is no external <see cref=\"IServiceProvider\" /> and EF is maintaining its own service\n    ///     provider internally. This allows database providers (and other extensions) to register their\n    ///     required services when EF is creating a service provider.\n    /// </summary>\n    /// <param name=\"services\">The collection to add services to.</param>\n    public abstract void ApplyServices(IServiceCollection services);\n\n    /// <summary>\n    ///     Gives the extension a chance to validate that all options in the extension are valid.\n    ///     Most extensions do not have invalid combinations and so this will be a no-op.\n    ///     If options are invalid, then an exception should be thrown.\n    /// </summary>\n    /// <param name=\"options\">The options being validated.</param>","sourceCodeStart":406,"sourceCodeEnd":442,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalOptionsExtension.cs#L406-L442","documentation":"Thrown by RelationalOptionsExtension.Extract(IDbContextOptions) at line 424 when more than one RelationalOptionsExtension is registered in the context options. EF only allows a single relational provider per context because each provider registers conflicting singleton services. The Count check `relationalOptionsExtensions.Count > 1` fires.","triggerScenarios":"Calling two provider registration methods on the same DbContext, e.g., optionsBuilder.UseSqlServer(connStr).UseSqlite(connStr). Or mixing UseNpgsql with UseSqlServer in different OnConfiguring paths that both execute.","commonSituations":"Copy-paste errors leaving two UseXxx calls. Provider switching where the old call wasn't removed. DI configuration that conditionally adds providers but the condition evaluates true for multiple branches. Integration test base classes that call UseInMemoryDatabase on top of a real provider.","solutions":["Remove all but one provider registration call from OnConfiguring / AddDbContext.","If selecting a provider at runtime, ensure only one branch executes (use else-if or a switch with a single call).","Check for a base class OnConfiguring that already registers a provider before calling another in the derived class."],"exampleFix":"// before: two providers registered\nprotected override void OnConfiguring(DbContextOptionsBuilder o)\n{\n    o.UseSqlServer(connStr);\n    o.UseSqlite(connStr); // duplicate\n}\n\n// after\nprotected override void OnConfiguring(DbContextOptionsBuilder o)\n{\n    o.UseSqlServer(connStr);\n}","handlingStrategy":"validation","validationCode":"var providerExts = context.Database.GetService<IDbContextOptions>().Extensions\n    .OfType<Microsoft.EntityFrameworkCore.Infrastructure.RelationalOptionsExtension>().ToList();\nif (providerExts.Count > 1) throw new InvalidOperationException($\"{providerExts.Count} relational providers registered — expected 1.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use if/else-if chains (not sequential ifs) for conditional provider selection so only one branch executes.","Audit OnConfiguring overrides in derived context classes for duplicate provider calls.","Add a startup test verifying exactly one relational extension is present."],"tags":["provider","configuration","startup"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}