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 showFix it fast
Most common cause, 1 of 3The 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.
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();
}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.
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.
grep -rnE "new [A-Za-z]+DbContext\(|: DbContext|DbContextOptions" --include=*.cs .Then find the line you see in this list and open its fix:
- 1The code creates the context with new instead of asking DI for it
A controller, service or background job contains new AppDbContext() with no options, while Program.cs registers AddDbContext - 2The constructor never hands the options to the base class
The context class has only a parameterless constructor, or it accepts the options but never calls : base(options) - 3dotnet ef builds the context without your app's services
The 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.
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.
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.
grep -rn "new AppDbContext()" --include=*.cs . || echo noneConfirmed by learn.microsoft.com
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.
grep -n -A3 "public AppDbContext(" Data/AppDbContext.csFix
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.
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.
dotnet runConfirmed by learn.microsoft.com
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.
dotnet ef migrations add Test --verboseFix
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.
// 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.
dotnet ef migrations listConfirmed by learn.microsoft.com
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.