dotnet/efcore · critical · InvalidOperationException

No relational database providers are configured. Configure a

Error message

No relational database providers are configured. Configure a database provider using 'OnConfiguring' or by creating an ImmutableDbContextOptions with a configured database provider and passing it to the context.

What it means

Thrown by RelationalOptionsExtension.Extract(IDbContextOptions) at line 422 when no RelationalOptionsExtension is found among the context's options extensions. This means no relational database provider (SqlServer, Npgsql, SQLite, etc.) has been registered via UseSqlServer/UseNpgsql/UseSqlite etc. The Extract method is used internally whenever relational-specific configuration is accessed.

Source

Thrown at src/EFCore.Relational/Infrastructure/RelationalOptionsExtension.cs:422

        return clone;
    }

    /// <summary>
    ///     Finds an existing <see cref="RelationalOptionsExtension" /> registered on the given options
    ///     or throws if none has been registered. This is typically used to find some relational
    ///     configuration when it is known that a relational provider is being used.
    /// </summary>
    /// <param name="options">The context options to look in.</param>
    /// <returns>The extension.</returns>
    public static RelationalOptionsExtension Extract(IDbContextOptions options)
    {
        var relationalOptionsExtensions
            = options.Extensions
                .OfType<RelationalOptionsExtension>()
                .ToList();

        return relationalOptionsExtensions.Count == 0
            ? throw new InvalidOperationException(RelationalStrings.NoProviderConfigured)
            : relationalOptionsExtensions.Count > 1
                ? throw new InvalidOperationException(RelationalStrings.MultipleProvidersConfigured)
                : relationalOptionsExtensions[0];
    }

    /// <summary>
    ///     Adds the services required to make the selected options work. This is used when there
    ///     is no external <see cref="IServiceProvider" /> and EF is maintaining its own service
    ///     provider internally. This allows database providers (and other extensions) to register their
    ///     required services when EF is creating a service provider.
    /// </summary>
    /// <param name="services">The collection to add services to.</param>
    public abstract void ApplyServices(IServiceCollection services);

    /// <summary>
    ///     Gives the extension a chance to validate that all options in the extension are valid.
    ///     Most extensions do not have invalid combinations and so this will be a no-op.
    ///     If options are invalid, then an exception should be thrown.

View on GitHub (pinned to dbf9771522)

Solutions

  1. Add a provider call in OnConfiguring: optionsBuilder.UseSqlServer(connectionString).
  2. If using DI: builder.Services.AddDbContext<MyContext>(o => o.UseSqlServer(connStr)).
  3. If this is a test, switch from UseInMemoryDatabase to UseSqlite/UseSqlServer or use the provider's in-memory/test double.
  4. Ensure conditional provider logic always registers exactly one provider.

Example fix

// before: no provider configured
public class MyContext : DbContext { }

// after
public class MyContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder o)
        => o.UseSqlServer(Configuration.GetConnectionString("Default"));
}
Defensive patterns

Strategy: validation

Validate before calling

// Assert at startup that a relational provider is registered.
using var context = scope.ServiceProvider.GetRequiredService<MyContext>();
var ext = context.Database.GetService<IDbContextOptions>().Extensions
    .OfType<Microsoft.EntityFrameworkCore.Infrastructure.RelationalOptionsExtension>().ToList();
if (ext.Count == 0) throw new InvalidOperationException("No relational provider registered.");

Prevention

When it happens

Trigger: Creating a DbContext without calling any Use<Provider>() in OnConfiguring or via AddDbContext options. Or calling relational-specific APIs (like getting a relational connection or migrations) on a context configured only with an in-memory provider (UseInMemoryDatabase).

Common situations: Forgetting to configure a provider in OnConfiguring or Program.cs. Using UseInMemoryDatabase for testing but then calling relational APIs. A DI registration that doesn't chain .UseSqlServer(). Conditional provider selection that falls through without registering any.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/3c8c43056c35e670. Report an issue: GitHub.