Created
June 11, 2021 10:37
-
-
Save thechandanbhagat/fd7b3eb9208214de9d3295f0dd3afb9f to your computer and use it in GitHub Desktop.
Seed function for student and admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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