System.InvalidOperationException: No database provider has been configured for this DbContext
means EF Core was asked to run a query on a context that was never told which database
to talk to. Give it a provider: override OnConfiguring and call
UseSqlServer (or UseNpgsql, UseSqlite), or register
the context with AddDbContext and let dependency injection create it.
In a console app or a quick test, the shortest fix is an OnConfiguring
override with a connection string. In an ASP.NET Core or Blazor app, register the context
with AddDbContext, add a constructor that takes
DbContextOptions<AppDbContext>, and stop creating it with
new.
The error
Unhandled exception. 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.
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, DbContextOptions contextOptions, DbContext context)
at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices()
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.Set[TEntity]()
at FixLab.Broken.AppDbContext.get_Customers() in ...\FixLab\Model.cs:line 41
at Program.<Main>$(String[] args) in ...\FixLab\Program.cs:line 9
at Program.<Main>(String[] args)
The two file paths were shortened; nothing else was changed. FixLab and its
Broken namespace are the test project's names.
Why it happens
A DbContext on its own knows your entities but not your database. The
provider (SQL Server, PostgreSQL, SQLite) and the connection string arrive through
DbContextOptions, and those options come from exactly two places: the
OnConfiguring override, or the options object that AddDbContext
builds and passes to the constructor. The context that failed here had neither:
public class AppDbContext : DbContext
{
public DbSet<Customer> Customers => Set<Customer>();
}
Notice where the stack trace points. new AppDbContext() succeeded; the
exception came from get_Customers(), the first time the code touched a
DbSet. EF Core builds the context's internal services lazily, on first use,
and that is the moment it discovers there is no provider. So the line in your trace is
usually the first query, not the place where the context was created.
The fix
public class AppDbContext : DbContext
{
public DbSet<Customer> Customers => Set<Customer>();
public DbSet<Order> Orders => Set<Order>();
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=Fix4Ef;Trusted_Connection=True;TrustServerCertificate=True");
}
dotnet run -- fixprov
Customers: 2
With OnConfiguring in place the same new AppDbContext() works; the
fixed run also called EnsureCreatedAsync to create the LocalDB database,
then counted the two seeded customers. In an app with dependency injection, prefer
AddDbContext so the connection string lives in configuration rather than in
code; the error message itself names the catch: the context then needs a constructor that
accepts DbContextOptions<TContext> and passes it to the base
constructor.
How it was reproduced
A console project from dotnet new console -n FixLab -f net10.0 on .NET SDK
10.0.401, with Microsoft.EntityFrameworkCore.SqlServer 10.0.12 and
Microsoft.EntityFrameworkCore.Design 10.0.12, against SQL Server 2022 LocalDB on Windows
11. A context with no OnConfiguring, created with new, threw on
CountAsync(); the same code with the override above printed the count.
Frequently asked
- How do I fix 'No database provider has been configured for this DbContext'?
- Give the context a provider. In a console app override OnConfiguring and call UseSqlServer, UseNpgsql or UseSqlite with a connection string. In an app with dependency injection register it with AddDbContext and give the context a constructor that takes DbContextOptions and passes it to base.
- Why does the error appear on the first query and not when I create the DbContext?
- EF Core builds a context's internal services lazily, the first time a DbSet or the database is used. Creating the context with new succeeds; the first query is where EF Core finds that no provider was configured.
- I call AddDbContext but still get 'No database provider has been configured'. Why?
- Either the context has no constructor that accepts DbContextOptions and passes it to the base DbContext, or the code creates the context with new instead of resolving it from dependency injection, so the registered options never reach it.
More decoded errors in the Fixes category. If the same
context also fails when you run dotnet ef migrations add, read
Unable to
create a DbContext of type, which is the design-time side of the same question.