'Unable to resolve service for type ApplicationContext

I have my ApplicationContext as following :-

You can see i am deriving ApplicationContext from ChildContext (child class) that in the end derives from `IdentityDbContext'.

public class ApplicationContext : ChildContext
{
    public ApplicationContext(DbContextOptions<ChildContext> options)
    : base(options)
    {
    }

    public DbSet<Class> Class { get; set; }
}


public  class ChildContext : IdentityDbContext<IfsUser>, IIFSContext
{
    public ChildContext(DbContextOptions<ChildContext> options)
    : base(options)
    {
    }

    public virtual DbSet<Student> Students { get; set; }
}

Startup.cs

services.AddDbContext<ApplicationContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("ApplicationDB")));

When i try to get instance

var context = services.GetRequiredService<ApplicationContext>();

I get the following error

Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[ChildContext]' while attempting to activate 'ApplicationContext'.



Solution 1:[1]

In your context's constructors you dont need to do this anymore

public ApplicationContext(DbContextOptions<ChildContext> options)
: base(options)
{
}

Just do

public ApplicationContext(DbContextOptions options)
: base(options)
{
}

Solution 2:[2]

I know there is an approved comment but I had a similar problem and I've resolved it this way just wanted to share.

I've added/modified the line in the startup.cs

services.AddDbContext<DbContext, ChildContext>(options => options.UseSqlServer(connectionString));

This line solved my problem. You might need to change the DbContext to ApplicationContext. Even though my ChildContext has the constructor is like shown below:

public ChildContext(DbContextOptions<ChildContext> options) : base (options)
{
  // enter your code here
}

Solution 3:[3]

I had the same issue and took me a some time to figure this out. My problem was that i had multiple projects. An API project where the Startup class is found and an Infrastructure project where i had the DbContext and want the migrations to be done.

To fix this you have to add an extra (-s) parameter to the dotnet-ef command to specify where the startup is found:

dotnet-ef migrations add -s ..\MyProject.Api\ InitialCreate --verbose

And in case you have multiple DbContexts you will also have to add a context parameter to specify which context you want to use:

dotnet-ef migrations add -s ..\MyProject.Api\ InitialCreate --context MyDbContext --verbose

A big thankd to LuisDev99! https://github.com/dotnet/efcore/issues/15145#issuecomment-725189870

The same is true when you want to run the update database command: dotnet-ef database update -s ..\GraydonService.Api\ InitialCreate --context MyDbContext --verbose

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 immirza
Solution 2
Solution 3 Lemraj