System.InvalidOperationException: Unable to resolve service for type 'FixLab.AppDbContext' while attempting to activate 'FixLab.CustomerService'.
means a class asks for your DbContext in its constructor, but the context was
never registered with dependency injection. Register it with
AddDbContext in Program.cs and the error goes away.
One line does it: builder.Services.AddDbContext<AppDbContext>(...) with
your provider and connection string. Registering the service that uses the context is not
enough; the container has to know how to build the context too.
The error
Unhandled exception. System.InvalidOperationException: Unable to resolve service for type 'FixLab.AppDbContext' while attempting to activate 'FixLab.CustomerService'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(ServiceIdentifier serviceIdentifier, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
The rest of the stack trace ends in GetRequiredService, called from
Program.cs. FixLab and CustomerService are the
test project's names; yours will show your own context and the class that needs it.
Why it happens
The message names two types. The second, CustomerService, is registered and
the container is trying to build it. The first, AppDbContext, is a
constructor parameter of that service, and the container has no registration for it.
The program registered only the service:
builder.Services.AddScoped<CustomerService>();
A DbContext is not registered by referencing the EF Core packages, and
creating it yourself elsewhere with new does not help either: the container
only hands out what was added to builder.Services. The error appears the
first time something resolves the service, which here was
GetRequiredService<CustomerService>(), not the line that built the
host.
The fix
builder.Services.AddDbContext<AppDbContext>(o => o.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=Fix4Ef;Trusted_Connection=True;TrustServerCertificate=True"));
dotnet run -- fixed
CustomerService resolved, provider: Microsoft.EntityFrameworkCore.SqlServer
AddDbContext registers the context as a scoped service, so each request or
scope gets its own instance. The context also needs a constructor that accepts the
options the container builds and passes them on:
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
Without that constructor the options never reach the context, and EF Core answers with "No database provider has been configured for this DbContext", whose own text points at this constructor. In a real app read the connection string from configuration rather than writing it in code.
How it was reproduced
A console project on .NET SDK 10.0.401 using the generic host
(Host.CreateApplicationBuilder) with Microsoft.Extensions.Hosting 10.0.12,
Microsoft.EntityFrameworkCore.SqlServer 10.0.12 and Microsoft.EntityFrameworkCore.Design
10.0.12 on Windows 11. CustomerService takes AppDbContext in its
constructor; without the AddDbContext line, resolving it threw the error
above, and with it the service resolved and reported the SQL Server provider.
Frequently asked
- How do I fix 'Unable to resolve service for type DbContext while attempting to activate'?
- Register the context in Program.cs with builder.Services.AddDbContext, passing your provider and connection string, and give the context a constructor that takes DbContextOptions of that context and passes it to base.
- Which type in the message is missing?
- The first one. The message reads: unable to resolve the first type while activating the second, so the first type is the missing registration and the second is the class that asked for it.
- Why does the error appear when the service is used and not at startup?
- The container builds a service the first time something asks for it. In the reproduction the exception came from GetRequiredService, two lines after the host was built, because nothing resolved CustomerService before that.
More decoded errors in the Fixes category. The same message for services other than a DbContext is covered in Unable to resolve service for type ... while attempting to activate.