Identity Rescafolding and DBContext changes

This commit is contained in:
Dimitar Todorov
2022-04-04 22:48:32 +03:00
parent 0ab907582c
commit 9b29101c2d
53 changed files with 1250 additions and 339 deletions

View File

@@ -6,21 +6,14 @@ using Microsoft.EntityFrameworkCore;
namespace Data
{
public class RentACarDbContext : IdentityDbContext<IdentityUser>
public class RentACarDbContext : IdentityDbContext<User, IdentityRole, string>
{
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)
@@ -33,57 +26,48 @@ namespace Data
protected override async void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
this.Database.EnsureCreated();
string[] roles = { "Admin", "Employee" };
foreach (string role in roles)
{
if (!await roleManager.RoleExistsAsync(role))
IdentityRole roleToCheck = await this.Roles.FirstOrDefaultAsync(roleToCheck => roleToCheck.Name == role);
if (roleToCheck == null)
{
await roleManager.CreateAsync(new IdentityRole(role));
//this.Roles.Add(new IdentityRole(role));
modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole(role));
}
}
User initialUser = new User
PasswordHasher<User> passwordHasher = new PasswordHasher<User>();
User initialUser = new User();
initialUser.Id = Guid.NewGuid().ToString();
initialUser.UserName = "admin";
initialUser.PasswordHash = passwordHasher.HashPassword(initialUser, "admin");
if (this.Users.FirstOrDefaultAsync() != null)
{
UserName = "admin",
PasswordHash = "admin",
};
modelBuilder.Entity<User>().HasData(initialUser);
await userManager.AddToRoleAsync(initialUser, roles[0]);
modelBuilder.Entity<User>().HasData(initialUser);
IdentityRole<string> adminRole = await this.Roles.FirstOrDefaultAsync(role => role.Name == "Admin");
modelBuilder.Entity<IdentityUserRole<string>>().HasData(new IdentityUserRole<string> {RoleId = adminRole.Id, UserId = initialUser.Id});
}
//modelBuilder.Entity<User>().HasData(
// new User
// {
// Username = "user",
// Password = "user",
// FirstName = "User",
// LastName = "User",
// PersonalNumber = "0987654321",
// PhoneNumber = "0882750588",
// Email = "user@gmail.org",
// Role = User.RoleEnum.User
// }
//);
//modelBuilder.Entity<User>().HasData(
// new User
// {
// Username = "manager",
// Password = "manager",
// FirstName = "Manager",
// LastName = "Manager",
// PersonalNumber = "0987654321",
// PhoneNumber = "0882750588",
// Email = "manager@gmail.org",
// Role = User.RoleEnum.Manager
// }
//);
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>()
.HasIndex(user => new { user.UserName })
.IsUnique(true);
modelBuilder.Entity<Rents>().HasOne(rents => rents.User);
modelBuilder.Entity<Rents>().HasOne(rents => rents.Car);
modelBuilder.Entity<Car>().HasData(new Car()
{
Id = 1,
Brand = "Trabant"
}) ;
this.SaveChanges();
}
}

View File

@@ -10,15 +10,15 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Data.Migrations
{
[DbContext(typeof(RentACarDbContext))]
[Migration("20220401141638_InitialMigration")]
partial class InitialMigration
[Migration("20220404172708_Changes")]
partial class Changes
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("ProductVersion", "5.0.13")
.HasAnnotation("ProductVersion", "5.0.15")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("Data.Entities.Car", b =>
@@ -58,7 +58,7 @@ namespace Data.Migrations
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<int?>("CarId")
b.Property<int>("CarId")
.HasColumnType("int");
b.Property<DateTime>("EndDate")
@@ -88,10 +88,12 @@ namespace Data.Migrations
.HasColumnType("int");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("nvarchar(max)");
b.Property<string>("Email")
.HasColumnType("nvarchar(max)");
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.Property<bool>("EmailConfirmed")
.HasColumnType("bit");
@@ -109,10 +111,12 @@ namespace Data.Migrations
.HasColumnType("datetimeoffset");
b.Property<string>("NormalizedEmail")
.HasColumnType("nvarchar(max)");
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.Property<string>("NormalizedUserName")
.HasColumnType("nvarchar(max)");
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.Property<string>("PasswordHash")
.HasColumnType("nvarchar(max)");
@@ -133,15 +137,20 @@ namespace Data.Migrations
.HasColumnType("bit");
b.Property<string>("UserName")
.HasColumnType("nvarchar(450)");
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.HasKey("Id");
b.HasIndex("UserName")
.IsUnique()
.HasFilter("[UserName] IS NOT NULL");
b.HasIndex("NormalizedEmail")
.HasDatabaseName("EmailIndex");
b.ToTable("User");
b.HasIndex("NormalizedUserName")
.IsUnique()
.HasDatabaseName("UserNameIndex")
.HasFilter("[NormalizedUserName] IS NOT NULL");
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
@@ -195,71 +204,6 @@ namespace Data.Migrations
b.ToTable("AspNetRoleClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b =>
{
b.Property<string>("Id")
.HasColumnType("nvarchar(450)");
b.Property<int>("AccessFailedCount")
.HasColumnType("int");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("nvarchar(max)");
b.Property<string>("Email")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.Property<bool>("EmailConfirmed")
.HasColumnType("bit");
b.Property<bool>("LockoutEnabled")
.HasColumnType("bit");
b.Property<DateTimeOffset?>("LockoutEnd")
.HasColumnType("datetimeoffset");
b.Property<string>("NormalizedEmail")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.Property<string>("NormalizedUserName")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.Property<string>("PasswordHash")
.HasColumnType("nvarchar(max)");
b.Property<string>("PhoneNumber")
.HasColumnType("nvarchar(max)");
b.Property<bool>("PhoneNumberConfirmed")
.HasColumnType("bit");
b.Property<string>("SecurityStamp")
.HasColumnType("nvarchar(max)");
b.Property<bool>("TwoFactorEnabled")
.HasColumnType("bit");
b.Property<string>("UserName")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.HasKey("Id");
b.HasIndex("NormalizedEmail")
.HasDatabaseName("EmailIndex");
b.HasIndex("NormalizedUserName")
.IsUnique()
.HasDatabaseName("UserNameIndex")
.HasFilter("[NormalizedUserName] IS NOT NULL");
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.Property<int>("Id")
@@ -344,7 +288,9 @@ namespace Data.Migrations
{
b.HasOne("Data.Entities.Car", "Car")
.WithMany()
.HasForeignKey("CarId");
.HasForeignKey("CarId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Data.Entities.User", "User")
.WithMany()
@@ -366,7 +312,7 @@ namespace Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null)
b.HasOne("Data.Entities.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
@@ -375,7 +321,7 @@ namespace Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null)
b.HasOne("Data.Entities.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
@@ -390,7 +336,7 @@ namespace Data.Migrations
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null)
b.HasOne("Data.Entities.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
@@ -399,7 +345,7 @@ namespace Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null)
b.HasOne("Data.Entities.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)

View File

@@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Data.Migrations
{
public partial class InitialMigration : Migration
public partial class Changes : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
@@ -26,6 +26,9 @@ namespace Data.Migrations
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
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),
UserName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
Email = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
@@ -64,34 +67,6 @@ namespace Data.Migrations
table.PrimaryKey("PK_Cars", x => x.Id);
});
migrationBuilder.CreateTable(
name: "User",
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
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),
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),
NormalizedEmail = table.Column<string>(type: "nvarchar(max)", nullable: true),
EmailConfirmed = table.Column<bool>(type: "bit", nullable: false),
PasswordHash = table.Column<string>(type: "nvarchar(max)", nullable: true),
SecurityStamp = table.Column<string>(type: "nvarchar(max)", nullable: true),
ConcurrencyStamp = table.Column<string>(type: "nvarchar(max)", nullable: true),
PhoneNumber = table.Column<string>(type: "nvarchar(max)", nullable: true),
PhoneNumberConfirmed = table.Column<bool>(type: "bit", nullable: false),
TwoFactorEnabled = table.Column<bool>(type: "bit", nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
LockoutEnabled = table.Column<bool>(type: "bit", nullable: false),
AccessFailedCount = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_User", x => x.Id);
});
migrationBuilder.CreateTable(
name: "AspNetRoleClaims",
columns: table => new
@@ -204,7 +179,7 @@ namespace Data.Migrations
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
CarId = table.Column<int>(type: "int", nullable: true),
CarId = table.Column<int>(type: "int", nullable: false),
StartDate = table.Column<DateTime>(type: "datetime2", nullable: false),
EndDate = table.Column<DateTime>(type: "datetime2", nullable: false),
UserId = table.Column<string>(type: "nvarchar(450)", nullable: true)
@@ -212,18 +187,18 @@ namespace Data.Migrations
constraints: table =>
{
table.PrimaryKey("PK_Rents", x => x.Id);
table.ForeignKey(
name: "FK_Rents_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Rents_Cars_CarId",
column: x => x.CarId,
principalTable: "Cars",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Rents_User_UserId",
column: x => x.UserId,
principalTable: "User",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
@@ -274,13 +249,6 @@ namespace Data.Migrations
name: "IX_Rents_UserId",
table: "Rents",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_User_UserName",
table: "User",
column: "UserName",
unique: true,
filter: "[UserName] IS NOT NULL");
}
protected override void Down(MigrationBuilder migrationBuilder)
@@ -311,9 +279,6 @@ namespace Data.Migrations
migrationBuilder.DropTable(
name: "Cars");
migrationBuilder.DropTable(
name: "User");
}
}
}

View File

@@ -16,7 +16,7 @@ namespace Data.Migrations
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("ProductVersion", "5.0.13")
.HasAnnotation("ProductVersion", "5.0.15")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("Data.Entities.Car", b =>
@@ -56,7 +56,7 @@ namespace Data.Migrations
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<int?>("CarId")
b.Property<int>("CarId")
.HasColumnType("int");
b.Property<DateTime>("EndDate")
@@ -86,10 +86,12 @@ namespace Data.Migrations
.HasColumnType("int");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("nvarchar(max)");
b.Property<string>("Email")
.HasColumnType("nvarchar(max)");
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.Property<bool>("EmailConfirmed")
.HasColumnType("bit");
@@ -107,10 +109,12 @@ namespace Data.Migrations
.HasColumnType("datetimeoffset");
b.Property<string>("NormalizedEmail")
.HasColumnType("nvarchar(max)");
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.Property<string>("NormalizedUserName")
.HasColumnType("nvarchar(max)");
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.Property<string>("PasswordHash")
.HasColumnType("nvarchar(max)");
@@ -131,15 +135,20 @@ namespace Data.Migrations
.HasColumnType("bit");
b.Property<string>("UserName")
.HasColumnType("nvarchar(450)");
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.HasKey("Id");
b.HasIndex("UserName")
.IsUnique()
.HasFilter("[UserName] IS NOT NULL");
b.HasIndex("NormalizedEmail")
.HasDatabaseName("EmailIndex");
b.ToTable("User");
b.HasIndex("NormalizedUserName")
.IsUnique()
.HasDatabaseName("UserNameIndex")
.HasFilter("[NormalizedUserName] IS NOT NULL");
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
@@ -193,71 +202,6 @@ namespace Data.Migrations
b.ToTable("AspNetRoleClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b =>
{
b.Property<string>("Id")
.HasColumnType("nvarchar(450)");
b.Property<int>("AccessFailedCount")
.HasColumnType("int");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("nvarchar(max)");
b.Property<string>("Email")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.Property<bool>("EmailConfirmed")
.HasColumnType("bit");
b.Property<bool>("LockoutEnabled")
.HasColumnType("bit");
b.Property<DateTimeOffset?>("LockoutEnd")
.HasColumnType("datetimeoffset");
b.Property<string>("NormalizedEmail")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.Property<string>("NormalizedUserName")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.Property<string>("PasswordHash")
.HasColumnType("nvarchar(max)");
b.Property<string>("PhoneNumber")
.HasColumnType("nvarchar(max)");
b.Property<bool>("PhoneNumberConfirmed")
.HasColumnType("bit");
b.Property<string>("SecurityStamp")
.HasColumnType("nvarchar(max)");
b.Property<bool>("TwoFactorEnabled")
.HasColumnType("bit");
b.Property<string>("UserName")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)");
b.HasKey("Id");
b.HasIndex("NormalizedEmail")
.HasDatabaseName("EmailIndex");
b.HasIndex("NormalizedUserName")
.IsUnique()
.HasDatabaseName("UserNameIndex")
.HasFilter("[NormalizedUserName] IS NOT NULL");
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.Property<int>("Id")
@@ -342,7 +286,9 @@ namespace Data.Migrations
{
b.HasOne("Data.Entities.Car", "Car")
.WithMany()
.HasForeignKey("CarId");
.HasForeignKey("CarId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Data.Entities.User", "User")
.WithMany()
@@ -364,7 +310,7 @@ namespace Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null)
b.HasOne("Data.Entities.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
@@ -373,7 +319,7 @@ namespace Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null)
b.HasOne("Data.Entities.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
@@ -388,7 +334,7 @@ namespace Data.Migrations
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null)
b.HasOne("Data.Entities.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
@@ -397,7 +343,7 @@ namespace Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null)
b.HasOne("Data.Entities.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)

View File

@@ -1,5 +1,7 @@
using System;
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -9,13 +11,13 @@ namespace Data.Entities
public class Rents
{
public int Id { get; set; }
[ForeignKey("Car")]
public int CarId { get; set; }
public virtual Car Car { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
[ForeignKey("User")]
public string UserId { get; set; }
public virtual User User { get; set; }
}
}