InvalidOperationException:尝试激活“ MyWebsite.Controllers.AccountController”时,无法解析类型为“ Microsoft.AspNetCore.Identity.UserManager`1 [MyWebsite.Models.User]”的服务。
访问/帐户/注册时出现此错误
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>();
//services.AddIdentity<User, IdentityRole>()
//.AddEntityFrameworkStores<ApplicationDbContext>();0
ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
Database.EnsureCreated();
}
}
班级用户
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
}
帐户控制器
private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager;
private readonly RoleManager<IdentityRole> _roleManager;
public AccountController(UserManager<User> userManager, SignInManager<User> signInManager, RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_signInManager = signInManager;
_roleManager = roleManager;
}
我是新来的,帮助
答案 0 :(得分:0)
您要为services.AddDefaultIdentity<IdentityUser>()
创建新的User
模型
因此您必须为该模型services.AddDefaultIdentity<User>()
添加服务
修改
确保您的IdentityRoles
服务正常运行
services.AddDefaultIdentity<User, IdentityRole>()