首页 > 解决方案 > 我无法在种子数据中创建管理员

问题描述

到目前为止我尝试了什么

public class SeedIdentity
{
    public static async Task Seed(IApplicationBuilder app)
    {   
        var identityContext = app.ApplicationServices.GetRequiredService<ApplicationIdentityDbContext>();
        identityContext.Database.Migrate();  

        if (!identityContext.Roles.Any(i => i.Name == "Admin" && i.Name == "User"))
        {
            var roleStore = new RoleStore<IdentityRole>(identityContext);
            var roleManager = new RoleManager<IdentityRole>(roleStore, null, null, null, null);


            var adminRole = new IdentityRole { Name = "admin" };
            var userRole = new IdentityRole { Name = "user" };
            await roleManager.CreateAsync(adminRole);
            await roleManager.CreateAsync(userRole);
            identityContext.SaveChanges();

        }

        if (!identityContext.Users.Any(i => i.UserName == "akinaldemir"))
        {
                var userStore = new UserStore<ApplicationUser>(identityContext);
                var userManager = new UserManager<ApplicationUser>(userStore, null, null, null,null, null, null, null, null);
                var admin = new ApplicationUser()
                {
                    UserName = "akinaldemir",
                    Email = "akinaldemir@gmail.com"
                };


                await userManager.CreateAsync(admin, "Abc12!jtre");
                await userManager.AddToRoleAsync(admin, "admin");

                identityContext.SaveChanges();


        }
    }
}

标签: c#asp.net-mvc

解决方案


推荐阅读