Search code examples
c#asp.net-coreasp.net-identityseeding

ASP.NET core Identity seed only creates the last user


It only creates a ADMIN user since this is the last user created in the code below.. Even though I step through and everything is Successful. Does this have something to do with me doing async wrong and it not finishing the first operation? (creating the user)

  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 = "[email protected]",
                UserFirstName = "Firstname",
                UserLastName = "LastName",
                Email = "[email protected]",
                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 = "[email protected]",
                UserFirstName = "Firstname",
                UserLastName = "LastName",
                Email = "[email protected]",
                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");

Solution

  • This code should be made async all the way and not mix async and blocking calls like .Result. The method signature should also be async Task and not 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("[email protected]") == null) {
                var user = new ApplicationUser {
                    UserName = "[email protected]",
                    UserFirstName = "Firstname",
                    UserLastName = "LastName",
                    Email = "[email protected]",
                    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("[email protected]") == null) {
                var admin = new ApplicationUser {
                    UserName = "[email protected]",
                    UserFirstName = "Firstname",
                    UserLastName = "LastName",
                    Email = "[email protected]",
                    UserSchool = "University of Maryland",
                    RefMedSchoolId = 1,
                    EmailConfirmed = false,
                    LockoutEnabled = false
                };
                var resultAdmin = await userManager.CreateAsync(admin, "Password123!");
                var resultAdministratorRole = await userManager.AddToRoleAsync(admin, "Administrator");
            }
    
            //...
        }
    }