I am building a MVC app in visual studio and want to seed users into the system who will occupy particular roles. I don't know what is wrong with the code or pgAdmin because the aspNetUsers table in the database does not seed (there are no entries). The roles seed, but not the aspNetUsers table, and consequently the AspNetUserRoles table does not seed either.
I placed a breakpoint on the seedUsers method, it does appear to run and execute all the methods with no problem. However, this is not reflected in the database.
Can anyone tell me what is going wrong and how I should go about fixing this issue (diagnosing)? Thank you.
The file where the seeding is done is pasted below:
public class DataService
{
private readonly ApplicationDbContext _dbContext;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly UserManager<SiteUser> _userManager;
public DataService(ApplicationDbContext dbContext,
RoleManager<IdentityRole> roleManager,
UserManager<SiteUser> userManager)
{
_dbContext = dbContext;
_roleManager = roleManager;
_userManager = userManager;
}
public async Task ManageDataAsync()
{
await SeedRolesAsync();
await SeedUsersAsync();
}
private async Task SeedRolesAsync()
{
if (_dbContext.Roles.Any())
{
return;
}
foreach (var role in Enum.GetNames(typeof(SiteRole)))
{
await _roleManager.CreateAsync(new IdentityRole(role));
}
}
private async Task SeedUsersAsync()
{
if (_dbContext.Users.Any())
{
return;
}
var adminUser = new SiteUser()
{
Email = "yy@gmail.com",
UserName = "y",
FirstName = "yy",
LastName = "mm",
PhoneNumber = "0444 44 4444",
EmailConfirmed = true
};
await _userManager.CreateAsync(adminUser, "password123");
await _userManager.AddToRoleAsync(adminUser, SiteRole.Administrator.ToString());
var modUser = new SiteUser()
{
Email = "xxx@gmail.com",
UserName = "xxs",
FirstName = "x",
LastName = "s",
PhoneNumber = "03 3333 3333",
EmailConfirmed = true
};
await _userManager.CreateAsync(modUser, "password123");
await _userManager.AddToRoleAsync(modUser, SiteRole.Moderator.ToString());
}
}
}