使用自定义IdentityUser类时出错

时间:2018-12-17 20:20:30

标签: c# asp.net-core

我在asp.net core 2.2中更改了表名,但出现以下错误

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.

我想我做了一切。这是我当前的代码。

services.AddDefaultIdentity<ApplicationUser>()
            .AddDefaultUI(UIFramework.Bootstrap4)
            .AddEntityFrameworkStores<ApplicationDbContext>();

这是我的dbcontext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Add your customizations after calling base.OnModelCreating(modelBuilder);
        builder.Entity<ApplicationUser>().ToTable("User");
        builder.Entity<IdentityRole>().ToTable("Role");
        builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaim");
        builder.Entity<IdentityUserRole<string>>().ToTable("UserRole");
        builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogin");
        builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaim");
        builder.Entity<IdentityUserToken<string>>().ToTable("UserToken");
    }
}

这是我的ApplicationUser模型

public class ApplicationUser : IdentityUser
{
}

我错过了什么?我相信我什么都有。

1 个答案:

答案 0 :(得分:0)

问题在您的Views\Shared\_LoginPartial.cshtml文件中。当前您的_LoginPartial.cshtml包含:

@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager

必须更改为:

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager