Unable to seed aspNetUsers table in pgAdmin4 through MVC app

lm1212 141 Reputation points
2022-04-04T09:43:18.91+00:00

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());
        }

    }
}
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,418 questions
Azure Database for PostgreSQL
{count} votes

Accepted answer
  1. AgaveJoe 27,696 Reputation points
    2022-04-05T12:39:48.673+00:00

    The default Identity template uses the email address as the username. If the seeded username is different than the email address then update the login page to accept a username input. The default validation is looking for an email address not a username.

    Scaffold the login page (and the create account page too) so you can update the source code include the username input if you have not done so already and this is a .NET Core project.

    Scaffold Identity in ASP.NET Core projects


0 additional answers

Sort by: Most helpful