Home > OS >  No service for type 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext'
No service for type 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext'

Time:05-10

While using the "AddIdentity" Extension Method i got the Error: 'No service for type 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext' has been registered.'

I added the IdentityUser to a ApplicationUser class

public class ApplicationUser : IdentityUser { }

Then i have configured the DbContext:

public class ReadDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<FileReadModel> Documents { get; set; }
    public DbSet<CategoryReadModel> Categories { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        modelBuilder.HasDefaultSchema("myschema");
        var configuration = new ReadConfiguration();
        modelBuilder.ApplyConfiguration<FileReadModel>(configuration);
        modelBuilder.ApplyConfiguration<CategoryReadModel>(configuration);
        base.OnModelCreating(modelBuilder);
    }
}

And finally calling the AddIdentity Extension Method.

var options = configuration.GetOptions<PostgresOptions>("Database");
services.AddDbContext<ReadDbContext>(ctx =>
            ctx.UseNpgsql(options.ConnectionString));
services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ReadDbContext>()
            .AddDefaultTokenProviders();

But at Startup I get the Error that the service type for IdentityDbContext could not be found. I tried to configure custom Roles and custom keys as well. I get the same error every time. What am I missing here?

CodePudding user response:

In somewhere you are going to inject wrong class,

as an example ;

@inject ManagerClass<IdentityDbContext> managerClass

to

@inject ManagerClass<ApplicationUser> managerClass

CodePudding user response:

I have found the solution. Something could not be cleanly removed from the bin or object folder. A cleanup did nothing, but manually deleting the compiled files did. After removing it runs with recompilation!

  • Related