Skip to content

Instantly share code, notes, and snippets.

@thechandanbhagat
Created June 11, 2021 10:37
Show Gist options
  • Save thechandanbhagat/fd7b3eb9208214de9d3295f0dd3afb9f to your computer and use it in GitHub Desktop.
Save thechandanbhagat/fd7b3eb9208214de9d3295f0dd3afb9f to your computer and use it in GitHub Desktop.
Seed function for student and admin
protected override void Seed(School.Web.Models.ApplicationDbContext db)
{
var roleStore = new RoleStore<IdentityRole>(db);
var roleManager = new RoleManager<IdentityRole>(roleStore);
if (!(db.Roles.Any(p => p.Name == "Admin")))
{
roleManager.Create(new IdentityRole() { Name = "Admin" });
}
if (!(db.Roles.Any(p => p.Name == "Student")))
{
roleManager.Create(new IdentityRole() { Name = "Student" });
}
if (!(db.Users.Any(u => u.UserName == "[email protected]")))
{
var userStore = new UserStore<ApplicationUser>(db);
var userManager = new UserManager<ApplicationUser>(userStore);
var userToInsert = new ApplicationUser { UserName = "[email protected]", PhoneNumber = "12345678911", Email = "[email protected]" };
userManager.Create(userToInsert, "Admin@123");
userManager.AddToRole(userToInsert.Id, "Admin");
}
if (!(db.Users.Any(u => u.UserName == "[email protected]")))
{
var userStore = new UserStore<ApplicationUser>(db);
var userManager = new UserManager<ApplicationUser>(userStore);
var userToInsert = new ApplicationUser { UserName = "[email protected]", PhoneNumber = "12345678911", Email = "[email protected]" };
userManager.Create(userToInsert, "Student@123");
userManager.AddToRole(userToInsert.Id, "Student");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment