{"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":"error","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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/RelationalOptionsExtension.cs#L406-L442","documentation":"RelationalOptionsExtension.Extract throws MultipleProvidersConfigured when more than one RelationalOptionsExtension is found in options.Extensions. A single DbContext may use exactly one relational provider; chaining two UseXxx calls (or adding two provider extensions) is ambiguous.","triggerScenarios":"Calling two provider registrations on the same options builder, e.g. optionsBuilder.UseSqlServer(...).UseNpgsql(...), or UseSqlite(...) followed by UseSqlServer(...). Also AddDbContext chain that conditionally adds two providers.","commonSituations":"Accidentally stacking provider calls; mixing a test provider (SQLite) with a production one (SQL Server) in the same configuration branch; copy-paste between environments.","solutions":["Keep exactly one UseXxx call per DbContext options build; remove the duplicate.","Branch with if/else or a switch on environment/config so only one provider is registered.","If you need two providers, use two separate DbContext types / options instances."],"exampleFix":"// before\noptionsBuilder.UseSqlServer(conn);\noptionsBuilder.UseNpgsql(conn); // -> [451]\n\n// after — pick one based on configuration\nif (usePostgres)\n{\n    optionsBuilder.UseNpgsql(conn);\n}\nelse\n{\n    optionsBuilder.UseSqlServer(conn);\n}","handlingStrategy":"validation","validationCode":"// Ensure at most one relational provider is registered.\nstatic int RelationalProviderCount(IDbContextOptions options)\n    => options.Extensions.OfType<RelationalOptionsExtension>().Count();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep exactly one UseXxx call per options build; remove duplicates.","Branch provider selection with if/else so only one registers per environment.","Use separate DbContext types if you truly need two providers."],"tags":["configuration","provider","relational"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}