Marius Vasile

Marius Vasile

  • 1.5k
  • 517
  • 5.5k

asp.net core seed data with startup

Jan 5 2021 7:00 PM
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
 
  1. using Microsoft.AspNetCore.Identity;  
  2. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;  
  3. using RoSafety.Data;  
  4. using RoSafety.Models;  
  5. using System.Linq;  
  6.   
  7. namespace RoSafety  
  8. {  
  9.     public class RoSafetyDbStartup  
  10.     {  
  11.         private readonly ApplicationDbContext _context;  
  12.   
  13.         public RoSafetyDbStartup(ApplicationDbContext context)  
  14.         {  
  15.             _context = context;  
  16.         }  
  17.   
  18.         public async void SeedAdminUser()  
  19.         {  
  20.             var user = new UserDataClass  
  21.             {  
  22.                 UserName = "weba@admin.com",  
  23.                 NormalizedUserName = "weba@admin.com",  
  24.                 Email = "weba@admin.com",  
  25.                 OrgID = "RS-001",  
  26.                 NormalizedEmail = "weba@admin.com",  
  27.                 EmailConfirmed = true,  
  28.                 LockoutEnabled = true  
  29.             };  
  30.             var roleStore = new RoleStore<UserRoleClass>(_context);  
  31.   
  32.             if(!_context.Roles.Any(r => r.Name == "AdminNew"))  
  33.             {  
  34.                 await roleStore.CreateAsync(new UserRoleClass { Name = "AdminNew", NormalizedName = "ADMINNEW" });  
  35.             }  
  36.             if(!_context.Users.Any(u => u.UserName == user.UserName))  
  37.             {  
  38.                 var password = new PasswordHasher<UserDataClass>();  
  39.                 var hashed = password.HashPassword(user, "vsdrg4gherrhb4re54#RFw");  
  40.                 user.PasswordHash = hashed;  
  41.                 var userStore = new UserStore<UserDataClass>(_context);  
  42.                 await userStore.CreateAsync(user);  
  43.                 await userStore.AddToRoleAsync(user, "AdminNew");  
  44.             }  
  45.   
  46.             await _context.SaveChangesAsync();  
  47.               
  48.         }  
  49.     }  
  50. }  
and in statup
 
  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RoSafetyDbStartup dbInit)  
  2.         {  
  3. .................................other data........................  
  4.   
  5. dbInit.SeedAdminUser();  
  6.   
  7. }  
but no data is updated in AspNetUsers or AspNetRoles on database-update
 
Oh, and my Identity is
 
  1. public class UserDataClass : IdentityUser  
  2.     {  
  3. ......some data.....  
  4. }  
  5.   
  6. public class UserRoleClass : IdentityRole  
  7.     {  
  8.         public virtual ICollection<AppIdentityRole> AppUserRoles { getset; }  
  9.     }  
  10.   
  11.     public class AppIdentityRole : IdentityUserRole<string>  
  12.     {  
  13.         public virtual UserDataClass User { getset; }  
  14.         public virtual UserRoleClass Role { getset; }  
  15.     }  
What am I doing wrong? 

Answers (10)