ASP.NET 核心身份种子只创建最后一个用户

ASP.NET core Identity seed only creates the last user

它只创建了一个 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");

这段代码应该一直异步,而不是像 .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");
        }

        //...
    }
}