public static async Task Initialize(AppDbContext context, UserManager <IdentityUser> userManager, RoleManager <IdentityRole> rolesManager) { context.Database.EnsureCreated(); var identityRoles = new string[] { "Admin", "Operator", "User" }; var identityUsers = new IdentityUser[] { new IdentityUser() { UserName = "******", Email = "*****@*****.**" }, new IdentityUser() { UserName = "******", Email = "*****@*****.**" }, new IdentityUser() { UserName = "******", Email = "*****@*****.**" }, }; var password = "******"; foreach (var role in identityRoles) { if (!await rolesManager.RoleExistsAsync(role)) { await rolesManager.CreateAsync(new IdentityRole(role)); } } foreach (var identityUser in identityUsers.Select((value, i) => new { i, value })) { if (await userManager.FindByNameAsync(identityUser.value.UserName) == null) { var result = userManager.CreateAsync(identityUser.value).Result; if (result.Succeeded) { await userManager.AddPasswordAsync(identityUser.value, password); await userManager.AddToRoleAsync(identityUser.value, identityRoles[identityUser.i]); } } } }