Seed database for Identity 2

前端 未结 4 1672
不知归路
不知归路 2020-12-08 11:20

I came across a problem for seeding the database with Identity v2. I separated out the IdentityModel from the MVC5 project to my Data Access Layer where I setup EF Migration

4条回答
  •  执念已碎
    2020-12-08 12:02

    This is the way to avoid using an OWIN context:

    protected override void Seed(Repository.DataContext.IdentityDb context)
        var roleStore = new RoleStore(context);
        var roleManager = new RoleManager(roleStore);
        var userStore = new UserStore(context);
        var userManager = new UserManager(userStore);               
        var user = new ApplicationUser { UserName = "sallen" };
    
        userManager.Create(user, "password");                    
        roleManager.Create(new IdentityRole { Name = "admin" });
        userManager.AddToRole(user.Id, "admin");
    }
    

提交回复
热议问题