Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

EF Core context has no provider even though AddDbContext is in Program.cs

Your error

System.InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider.2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The code creates the context with new instead of asking DI for it

Take the context as a constructor parameter and let dependency injection supply it.

Fix
public class OrdersService
{
    private readonly AppDbContext _db;
    public OrdersService(AppDbContext db) => _db = db;   // injected, already configured

    public Task<List<Order>> OpenOrders() =>
        _db.Orders.Where(o => !o.Closed).ToListAsync();
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

No database provider has been configured for this DbContext means the instance EF Core is using never received UseSqlServer, UseNpgsql or a similar call. On EF Core 10 and 11 the usual reasons are a context created with new, a constructor that does not pass DbContextOptions<TContext> to base, or dotnet ef falling back to a parameterless constructor at design time.

  • Applies to EF Core 11.0.0 (.NET 11), EF Core 10.0 LTS (.NET 10), dotnet-ef tools 11.0.0
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Search for every place the context is created or declared. A hit for new AppDbContext() points to cause 1, a constructor without DbContextOptions<AppDbContext> to cause 2, and an error that only appears when you run dotnet ef to cause 3.

Run this first
grep -rnE "new [A-Za-z]+DbContext\(|: DbContext|DbContextOptions" --include=*.cs .

Then find the line you see in this list and open its fix:

  1. 1The code creates the context with new instead of asking DI for itA controller, service or background job contains new AppDbContext() with no options, while Program.cs registers AddDbContext
  2. 2The constructor never hands the options to the base classThe context class has only a parameterless constructor, or it accepts the options but never calls : base(options)
  3. 3dotnet ef builds the context without your app's servicesThe app itself runs fine, and the error appears only from dotnet ef migrations add or dotnet ef database update

What does the error look like?

  • System.InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider.
  • If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
  • This is often because no database provider has been configured for this DbContext.

Cause 1 of 3

The code creates the context with new instead of asking DI for it

You seeA controller, service or background job contains new AppDbContext() with no options, while Program.cs registers AddDbContext

Check

AddDbContext configures only the instances that the service provider builds. A context made with new and no options skips that setup, so EF Core finds no provider and throws this InvalidOperationException on the first query.

Check
grep -rn "new AppDbContext()" --include=*.cs .

Fix

Take the context as a constructor parameter and let dependency injection supply it. Where no service scope matches the work, as in Blazor components, register AddDbContextFactory and create short-lived contexts from it instead.

Fix
public class OrdersService
{
    private readonly AppDbContext _db;
    public OrdersService(AppDbContext db) => _db = db;   // injected, already configured

    public Task<List<Order>> OpenOrders() =>
        _db.Orders.Where(o => !o.Closed).ToListAsync();
}

Verify

The grep finds no parameterless new AppDbContext() left, and the endpoint that failed returns data.

Verify
grep -rn "new AppDbContext()" --include=*.cs . || echo none

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

The constructor never hands the options to the base class

You seeThe context class has only a parameterless constructor, or it accepts the options but never calls : base(options)

Check

Microsoft's configuration guide states the context must expose a public constructor that takes DbContextOptions<TContext>; that is how the settings from AddDbContext reach DbContext. The error text gives the same advice in its last sentence, which is the clue that this is the case.

Check
grep -n -A3 "public AppDbContext(" Data/AppDbContext.cs

Fix

Declare one public constructor with the generic options type and pass it to base. Remove an empty constructor that DI or the tools could pick instead.

Fix
public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
    {
    }

    public DbSet<Order> Orders => Set<Order>();
}

Verify

The application starts and the first query runs against the database named in your connection string.

Verify
dotnet run

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 3

dotnet ef builds the context without your app's services

You seeThe app itself runs fine, and the error appears only from dotnet ef migrations add or dotnet ef database update

Check

The EF Core tools first try to get the context from the startup project's service provider. If they cannot, for example because the startup project is a class library, they look for a constructor with no parameters, and that instance has no provider unless OnConfiguring sets one.

Check
dotnet ef migrations add Test --verbose

Fix

Point the tools at the project that calls AddDbContext with --startup-project, or add a design-time factory, which the tools use ahead of every other creation pattern. Configure it with the same provider the app uses.

Fix
// Option A: run the tools against the web project
// dotnet ef migrations add Init --project Data --startup-project Web

// Option B: a design-time factory in the context's project
public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
    public AppDbContext CreateDbContext(string[] args)
    {
        var options = new DbContextOptionsBuilder<AppDbContext>();
        options.UseSqlite("Data Source=app.db");
        return new AppDbContext(options.Options);
    }
}

Verify

dotnet ef migrations add finishes and writes a new file under Migrations.

Verify
dotnet ef migrations list

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Ban new SomeDbContext() outside tests and design-time factories with a code review rule or an analyzer.
  • Keep exactly one public constructor on each context, the one that takes DbContextOptions<TContext>.
  • Run dotnet ef migrations list in CI, so a broken design-time setup fails the build before someone needs a migration.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Raheeb, Loade Driver, Dhero.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.