这些是我所做的改变:
使用我自己的DbContext:
public class DataContext:IdentityDbContext
我修改了OnModelCreating方法
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
这是种子方法:
protected override void Seed(DataContext context)
{
var UserManager = new UserManager<User>(new UserStore<User>(context));
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
IdentityResult roleResult;
if (!RoleManager.RoleExists("Admin"))
{
roleResult = null;
roleResult = RoleManager.Create(new IdentityRole("Admin"));
if (roleResult.Succeeded)
{
User admin = new User { UserName = "Admin" };
UserManager.Create(admin, "Password1");
}
}
}
但是当我尝试登录时,我收到此错误:&#34;对象引用未设置为对象的实例。&#34;
Line 60: if (ModelState.IsValid)
Line 61: {
Line 62: var user = await UserManager.FindAsync(model.UserName, model.Password);