I am trying to seed data at startup in preparation for publishing. I want to have the web admin seeded at startup. What I did is
a class
- using Microsoft.AspNetCore.Identity;
- using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
- using RoSafety.Data;
- using RoSafety.Models;
- using System.Linq;
- namespace RoSafety
- {
- public class RoSafetyDbStartup
- {
- private readonly ApplicationDbContext _context;
- public RoSafetyDbStartup(ApplicationDbContext context)
- {
- _context = context;
- }
- public async void SeedAdminUser()
- {
- var user = new UserDataClass
- {
- UserName = "[email protected]",
- NormalizedUserName = "[email protected]",
- Email = "[email protected]",
- OrgID = "RS-001",
- NormalizedEmail = "[email protected]",
- EmailConfirmed = true,
- LockoutEnabled = true
- };
- var roleStore = new RoleStore
(_context); - if(!_context.Roles.Any(r => r.Name == "AdminNew"))
- {
- await roleStore.CreateAsync(new UserRoleClass { Name = "AdminNew", NormalizedName = "ADMINNEW" });
- }
- if(!_context.Users.Any(u => u.UserName == user.UserName))
- {
- var password = new PasswordHasher
(); - var hashed = password.HashPassword(user, "vsdrg4gherrhb4re54#RFw");
- user.PasswordHash = hashed;
- var userStore = new UserStore
(_context); - await userStore.CreateAsync(user);
- await userStore.AddToRoleAsync(user, "AdminNew");
- }
- await _context.SaveChangesAsync();
- }
- }
- }
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RoSafetyDbStartup dbInit)
- {
- .................................other data........................
- dbInit.SeedAdminUser();
- }
Oh, and my Identity is
- public class UserDataClass : IdentityUser
- {
- ......some data.....
- }
- public class UserRoleClass : IdentityRole
- {
- public virtual ICollection
AppUserRoles { get; set; } - }
- public class AppIdentityRole : IdentityUserRole<string>
- {
- public virtual UserDataClass User { get; set; }
- public virtual UserRoleClass Role { get; set; }
- }


Kiran MohantyRank #821 · 2.8K reputationPosted Jan 6, 2021, 1:11 AM EST
Marius VasileRank #785 · 2.9K reputationPosted Jan 6, 2021, 4:38 AM EST
Marius VasileRank #785 · 2.9K reputationPosted Jan 6, 2021, 1:24 AM EST
Kiran MohantyRank #821 · 2.8K reputationPosted Jan 6, 2021, 2:04 AM EST
Kiran MohantyRank #821 · 2.8K reputationPosted Jan 6, 2021, 2:13 AM EST
Marius VasileRank #785 · 2.9K reputationPosted Jan 6, 2021, 2:44 AM EST
https://stackoverflow.com/questions/34343599/how-to-seed-users-and-roles-with-code-first-migration-using-identity-asp-net-corI based my approach on that but is not working and I don't know what is wrong. The Identity table is there with data but not updated with seed data. Anyway, what trigger the seed? update-database command is enough? how do i trigger the update when I upload the files on the server? I don't know that eitherKiran MohantyRank #821 · 2.8K reputationPosted Jan 6, 2021, 3:19 AM EST
Marius VasileRank #785 · 2.9K reputationPosted Jan 6, 2021, 3:34 AM EST
Kiran MohantyRank #821 · 2.8K reputationPosted Jan 6, 2021, 12:45 AM EST
Marius VasileRank #785 · 2.9K reputationPosted Jan 6, 2021, 12:58 AM EST