Fixed errors on website loading
Fixed the errors that occurred on initial website load by moving the initial user creation in the DBContext and not in the ASP.NET project
This commit is contained in:
@@ -10,27 +10,47 @@ namespace Data
|
||||
{
|
||||
public virtual DbSet<Car> Cars { get; set; }
|
||||
public virtual DbSet<Rents> Rents { get; set; }
|
||||
private UserManager<User> userManager { get; set; }
|
||||
private RoleManager<IdentityRole> roleManager { get; set; }
|
||||
|
||||
public RentACarDbContext()
|
||||
{
|
||||
|
||||
//TODO: initialize UserManager and RoleManager
|
||||
}
|
||||
|
||||
public RentACarDbContext(DbContextOptions<RentACarDbContext> dbContextOptions) : base(dbContextOptions)
|
||||
{
|
||||
|
||||
//TODO: initialize UserManager and RoleManager
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (!optionsBuilder.IsConfigured)
|
||||
{
|
||||
optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=RentACar;Integrated Security=true;");
|
||||
optionsBuilder.UseSqlServer("Server=.;Database=RentACar;Integrated Security=true;");
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
protected override async void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
string[] roles = { "Admin", "Employee" };
|
||||
|
||||
foreach (string role in roles)
|
||||
{
|
||||
if (!await roleManager.RoleExistsAsync(role))
|
||||
{
|
||||
await roleManager.CreateAsync(new IdentityRole(role));
|
||||
}
|
||||
}
|
||||
|
||||
User initialUser = new User
|
||||
{
|
||||
UserName = "admin",
|
||||
PasswordHash = "admin",
|
||||
};
|
||||
|
||||
modelBuilder.Entity<User>().HasData(initialUser);
|
||||
await userManager.AddToRoleAsync(initialUser, roles[0]);
|
||||
|
||||
//modelBuilder.Entity<User>().HasData(
|
||||
// new User
|
||||
|
||||
@@ -10,8 +10,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
namespace Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(RentACarDbContext))]
|
||||
[Migration("20220401132719_initial")]
|
||||
partial class initial
|
||||
[Migration("20220401141638_InitialMigration")]
|
||||
partial class InitialMigration
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
@@ -99,9 +99,6 @@ namespace Data.Migrations
|
||||
b.Property<string>("FirstName")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<bool>("IsAdmin")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<string>("LastName")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
@@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace Data.Migrations
|
||||
{
|
||||
public partial class initial : Migration
|
||||
public partial class InitialMigration : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
@@ -72,7 +72,6 @@ namespace Data.Migrations
|
||||
FirstName = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
LastName = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
PersonalNumber = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
IsAdmin = table.Column<bool>(type: "bit", nullable: false),
|
||||
UserName = table.Column<string>(type: "nvarchar(450)", nullable: true),
|
||||
NormalizedUserName = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
Email = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
@@ -97,9 +97,6 @@ namespace Data.Migrations
|
||||
b.Property<string>("FirstName")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<bool>("IsAdmin")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<string>("LastName")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user