forked from bhrugen/Bulky
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Section 13 - Implement DbInitializer
- Loading branch information
Showing
4 changed files
with
72 additions
and
11 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
using System; | ||
using BulkyBook.Models; | ||
using BulkyBook.Utility; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
@@ -8,14 +12,64 @@ namespace BulkyBook.DataAccess.DbInitializer | |
{ | ||
public class DbInitializer : IDbInitializer | ||
{ | ||
private readonly UserManager<IdentityUser> _userManager; | ||
private readonly RoleManager<IdentityRole> _roleManager; | ||
private readonly ApplicationDbContext _db; | ||
|
||
public DbInitializer( | ||
UserManager<IdentityUser> userManager, | ||
RoleManager<IdentityRole> roleManager, | ||
ApplicationDbContext db) | ||
{ | ||
_roleManager = roleManager; | ||
_userManager = userManager; | ||
_db = db; | ||
} | ||
|
||
|
||
public void Initialize() | ||
{ | ||
//migrations if they are not applied | ||
try | ||
{ | ||
if (_db.Database.GetPendingMigrations().Count() > 0) | ||
{ | ||
_db.Database.Migrate(); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
|
||
} | ||
|
||
//create roles if they are not created | ||
if (!_roleManager.RoleExistsAsync(SD.Role_Admin).GetAwaiter().GetResult()) | ||
{ | ||
_roleManager.CreateAsync(new IdentityRole(SD.Role_Admin)).GetAwaiter().GetResult(); | ||
_roleManager.CreateAsync(new IdentityRole(SD.Role_Employee)).GetAwaiter().GetResult(); | ||
_roleManager.CreateAsync(new IdentityRole(SD.Role_User_Indi)).GetAwaiter().GetResult(); | ||
_roleManager.CreateAsync(new IdentityRole(SD.Role_User_Comp)).GetAwaiter().GetResult(); | ||
|
||
//if roles are not created, then we will create admin user as well | ||
|
||
_userManager.CreateAsync(new ApplicationUser | ||
{ | ||
UserName = "[email protected]", | ||
Email = "[email protected]", | ||
Name = "Bhrugen Patel", | ||
PhoneNumber = "1112223333", | ||
StreetAddress = "test 123 Ave", | ||
State = "IL", | ||
PostalCode = "23422", | ||
City = "Chicago" | ||
}, "Admin123*").GetAwaiter().GetResult(); | ||
|
||
ApplicationUser user = _db.ApplicationUsers.FirstOrDefault(u => u.Email == "[email protected]"); | ||
|
||
_userManager.AddToRoleAsync(user, SD.Role_Admin).GetAwaiter().GetResult(); | ||
|
||
//if roles are not created, then we will create admin user as well | ||
|
||
} | ||
return; | ||
} | ||
} | ||
} |
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
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