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 MohantyPosted Jan 6, 2021, 6:11 AM
Marius VasilePosted Jan 6, 2021, 9:38 AM
Marius VasilePosted Jan 6, 2021, 8:34 AM
Kiran MohantyPosted Jan 6, 2021, 8:19 AM
Marius VasilePosted Jan 6, 2021, 7:44 AM
I 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 MohantyPosted Jan 6, 2021, 7:13 AM
Kiran MohantyPosted Jan 6, 2021, 7:04 AM
Marius VasilePosted Jan 6, 2021, 6:24 AM
Marius VasilePosted Jan 6, 2021, 5:58 AM
Kiran MohantyPosted Jan 6, 2021, 5:45 AM