I have been browsing internet the whole day and tried whatever I could already. I have no errors and user is created into database but there is no UserRole assigned. Also roles are created just fine and can be visible in database. Why so? Can somebody see what’s the problem in here?
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
Guid ADMIN_ID = Guid.NewGuid();
Guid ROLE_ID = ADMIN_ID;
builder.Entity<ApplicationRole>().HasData(new ApplicationRole { Name = "User", NormalizedName = "USER", Id = Guid.NewGuid(), ConcurrencyStamp = Guid.NewGuid().ToString() });
builder.Entity<ApplicationRole>().HasData(new ApplicationRole { Name = "Admin", NormalizedName = "ADMIN", Id = ROLE_ID, ConcurrencyStamp = ROLE_ID.ToString() });
PasswordHasher<ApplicationUser> hasher = new PasswordHasher<ApplicationUser>();
builder.Entity<ApplicationUser>().HasData(new ApplicationUser
{
Id = ADMIN_ID,
FirstName = "MrJack",
LastName = "Jackson",
UserName = "Administrator",
NormalizedUserName = "Administrator",
Email = "[email protected]",
IsActive = true,
ProfilePicture = System.IO.File.ReadAllBytes(string.Concat(Path.GetFullPath("Resources\"), "avatar.png")),
NormalizedEmail = "[email protected]",
EmailConfirmed = true,
PasswordHash = hasher.HashPassword(null, "QWERTY"),
SecurityStamp = string.Empty,
ConcurrencyStamp = Guid.NewGuid().ToString(),
});
builder.Entity<IdentityUserRole<Guid>>().HasData(new IdentityUserRole<Guid>
{
RoleId = ROLE_ID,
UserId = ADMIN_ID
});
builder.Entity<IdentityUserRole<Guid>>().HasKey(p => new { p.UserId, p.RoleId });
}
I think problem is in this part, as user is not assigned in DB to user. This table is empty for some reason, but what is actually the problem?
builder.Entity<IdentityUserRole<Guid>>().HasData(new IdentityUserRole<Guid>
{
RoleId = ROLE_ID,
UserId = ADMIN_ID
});
builder.Entity<IdentityUserRole<Guid>>().HasKey(p => new { p.UserId, p.RoleId });
1 Answer
Not sure if this will solve your issue but I’ve noticed that you are assigning new GUID value to ADMIN_ID and then basically passing the same value to ROLE_ID.
Assign a new unique value to ROLE_ID then retry the seed.
mcatic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.