Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions SQLQuery1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
select * from dbo.AspNetUsers
select * from dbo.AspNetUserRoles
select * from dbo.AspNetRoles
select * from dbo.ShoppingCarts
select * from dbo.Companies
select * from dbo.Categories
select * from dbo.Products
select * from dbo.CoverType
select * from dbo.OrderHeaders
17 changes: 12 additions & 5 deletions XYZStore.DataAccess/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using XYZStore.DataAccess;
using XYZStore.Models;
using XYZStore.Models.Models;

namespace XYZStore.DataAccess;

public class ApplicationDbContext : DbContext
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext>options) : base(options)
{

}
public DbSet<Category>Categories { get; set; }
public DbSet<CoverType>CoverType { get; set; }
public DbSet<Product>Products { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<CoverType> CoverType { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<Company> Companies { get; set; }
public DbSet<ShoppingCart> ShoppingCarts { get; set; }
public DbSet<OrderHeader> OrderHeaders { get; set; }
public DbSet<OrderDetail> OrderDetails { get; set; }

}
67 changes: 67 additions & 0 deletions XYZStore.DataAccess/DbInitializer/DbInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using XYZStore.Models;
using XYZStore.Utility;

namespace XYZStore.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()
{
//apply 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 = "AdminoUser@outlook.com",
Email = "AdminoUser@outlook.com",
Name = "Admin Master",
PhoneNumber = "121212131313",
StreetAddress = "TownStreet",
State = "SWE",
PostalCode = "60666",
City = "Town"
}, "Admin123%").GetAwaiter().GetResult();
ApplicationUser user = _db.ApplicationUsers.FirstOrDefault(u => u.Email == "AdminoUser@outlook.com");


_userManager.AddToRoleAsync(user, SD.Role_Admin).GetAwaiter().GetResult();
}
return;
}
}
}
13 changes: 13 additions & 0 deletions XYZStore.DataAccess/DbInitializer/IDbInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XYZStore.DataAccess.DbInitializer
{
public interface IDbInitializer
{
void Initialize();
}
}
Loading