Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.

like image 238
simbada Avatar asked Sep 18 '25 09:09

simbada


1 Answers

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)
{
}
like image 156
immirza Avatar answered Sep 21 '25 01:09

immirza