ASP.NET核心标识种子仅创建最后一个用户

时间:2018-05-21 14:18:14

标签: c# asp.net-core asp.net-identity seeding

它只创建一个ADMIN用户,因为这是在下面的代码中创建的最后一个用户。即使我单步执行,一切都成功。这是否与我做异步错误并且没有完成第一次操作有关? (创建用户)

  public class DbInitializer
    {
        public static async void Initialize(IApplicationBuilder app)
       // public static void Initialize(IApplicationBuilder app)

... ...

   if (!roleManager.RoleExistsAsync("Administrator").Result)
        {
            IdRoleResult = await roleManager.CreateAsync(new IdentityRole("Administrator"));
          //  IdRoleResult =  roleManager.CreateAsync(new IdentityRole("Administrator")).Result;


            if (!IdRoleResult.Succeeded)
                throw new Exception("Administrator role wasnt created.");
        }

        if (!roleManager.RoleExistsAsync("User").Result)
        {
            IdUserResult = await roleManager.CreateAsync(new IdentityRole("User"));
            //  IdUserResult =  roleManager.CreateAsync(new IdentityRole("User")).Result;


            if (!IdUserResult.Succeeded)
                throw new Exception("User role wasnt created.");
        }

        //If there are no users, create a test user and test admin. Assign roles
        if (!context.Users.Any())
        {
            var user = new ApplicationUser
            {
                UserName = "user@user.com",
                UserFirstName = "Firstname",
                UserLastName = "LastName",
                Email = "user@user.com",
                UserSchool = "University of Maryland",
                RefMedSchoolId = 1,
                EmailConfirmed = false,
                LockoutEnabled = false
            };
            //  var resultUser = userManager.CreateAsync(user, "Password123!").Result;
            // var resultUserRole = userManager.AddToRoleAsync(user, "User").Result;
              var resultUser = await userManager.CreateAsync(user, "Password123!");
             var resultUserRole = await userManager.AddToRoleAsync(user, "User");

            var admin = new ApplicationUser
            {
                UserName = "admin@admin.com",
                UserFirstName = "Firstname",
                UserLastName = "LastName",
                Email = "admin@admin.com",
                UserSchool = "University of Maryland",
                RefMedSchoolId = 1,
                EmailConfirmed = false,
                LockoutEnabled = false
            };
           // var resultAdmin = userManager.CreateAsync(admin, "Password123!").Result;
          //  var resultAdministratorRole = userManager.AddToRoleAsync(admin, "Administrator").Result;

            var resultAdmin = await userManager.CreateAsync(admin, "Password123!");
            var resultAdministratorRole = await userManager.AddToRoleAsync(admin, "Administrator");

1 个答案:

答案 0 :(得分:4)

此代码应始终为异步,而不是混合异步和阻塞调用,如.Result。 方法签名也应该是async Task而不是async void

public class DbInitializer {
    public static async Task Initialize(IApplicationBuilder app) {

        //... ...

        if (! await roleManager.RoleExistsAsync("Administrator")) {
            IdRoleResult = await roleManager.CreateAsync(new IdentityRole("Administrator"));
            if (!IdRoleResult.Succeeded)
                throw new Exception("Administrator role wasnt created.");
        }

        if (! await roleManager.RoleExistsAsync("User")) {
            IdUserResult = await roleManager.CreateAsync(new IdentityRole("User"));
            if (!IdUserResult.Succeeded)
                throw new Exception("User role wasnt created.");
        }

        //If there are no test user, create a test user and assign roles
        if (await userManager.FindByNameAsync("user@user.com") == null) {
            var user = new ApplicationUser {
                UserName = "user@user.com",
                UserFirstName = "Firstname",
                UserLastName = "LastName",
                Email = "user@user.com",
                UserSchool = "University of Maryland",
                RefMedSchoolId = 1,
                EmailConfirmed = false,
                LockoutEnabled = false
            };
            var resultUser = await userManager.CreateAsync(user, "Password123!");
            var resultUserRole = await userManager.AddToRoleAsync(user, "User");
        }

        //If there are no test admin, create a test admin and assign roles
        if (await userManager.FindByNameAsync("admin@admin.com") == null) {
            var admin = new ApplicationUser {
                UserName = "admin@admin.com",
                UserFirstName = "Firstname",
                UserLastName = "LastName",
                Email = "admin@admin.com",
                UserSchool = "University of Maryland",
                RefMedSchoolId = 1,
                EmailConfirmed = false,
                LockoutEnabled = false
            };
            var resultAdmin = await userManager.CreateAsync(admin, "Password123!");
            var resultAdministratorRole = await userManager.AddToRoleAsync(admin, "Administrator");
        }

        //...
    }
}