Lazy Loading fix, new migration, Rents Index data

This commit is contained in:
Dimitar Byalkov
2022-04-09 03:21:47 +03:00
parent 21117225b7
commit 9c857a9458
14 changed files with 143 additions and 92 deletions

View File

@@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.12" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="5.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.12">
<PrivateAssets>all</PrivateAssets>

View File

@@ -20,7 +20,7 @@ namespace Data
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=.\\SQLEXPRESS;Database=RentACar;Integrated Security=true;");
optionsBuilder.UseSqlServer("Server=.\\SQLEXPRESS;Database=RentACar;Integrated Security=true;").UseLazyLoadingProxies();
}
}
@@ -63,7 +63,6 @@ namespace Data
modelBuilder.Entity<Car>().HasKey(car => car.Id);
modelBuilder.Entity<Rents>().HasOne(rents => rents.User);
modelBuilder.Entity<Rents>().HasOne(rents => rents.Car);
base.OnModelCreating(modelBuilder);
}

View File

@@ -10,8 +10,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Data.Migrations
{
[DbContext(typeof(RentACarDbContext))]
[Migration("20220408211811_initial")]
partial class initial
[Migration("20220408234818_Initial")]
partial class Initial
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
@@ -68,7 +68,6 @@ namespace Data.Migrations
.HasColumnType("datetime2");
b.Property<string>("UserId")
.IsRequired()
.HasColumnType("nvarchar(450)");
b.HasKey("Id");
@@ -156,15 +155,15 @@ namespace Data.Migrations
b.HasData(
new
{
Id = "28c75ddc-f972-4532-be46-adae03df32fb",
Id = "691a45ed-4399-47a1-a8c2-2a2abac9ba74",
AccessFailedCount = 0,
ConcurrencyStamp = "33c93ad2-8f61-4e3c-8d61-1583ea02a347",
ConcurrencyStamp = "c91c153e-ba2e-4da9-a397-5ac6f098d369",
Email = "admin@admin.admin",
EmailConfirmed = false,
LockoutEnabled = false,
PasswordHash = "AQAAAAEAACcQAAAAEMzxMVddhY+nvlkkmN6QRcuyj3Vh4iVvZ9xMarXBCh1APRAhWUU90mP4SOf0qS3A8Q==",
PasswordHash = "AQAAAAEAACcQAAAAEBajAJZYLZCUESCrgsH15V9n0OFqo9MLHWVzA3zHo7aqKbmSoUCv1TAu9j3r+PC/mw==",
PhoneNumberConfirmed = false,
SecurityStamp = "f548cc6f-ef08-4714-9c7a-e4e660cdced3",
SecurityStamp = "b105af8c-ee72-4969-9ba5-98bd13897459",
TwoFactorEnabled = false,
UserName = "admin"
});
@@ -199,14 +198,14 @@ namespace Data.Migrations
b.HasData(
new
{
Id = "14ca6e11-5721-43c8-a170-b74dbd8dd7f8",
ConcurrencyStamp = "18c88b14-89fd-4563-927c-23ed1da3d868",
Id = "8af8225c-ac26-406b-9d29-c172f95240c0",
ConcurrencyStamp = "446df763-47b2-4b18-b196-86f63f2a6a8b",
Name = "Admin"
},
new
{
Id = "b79e8165-7e4a-409f-8d27-455dd03ca398",
ConcurrencyStamp = "32443d24-2ba0-4d21-90f4-40971854864c",
Id = "b006be23-eb9f-480d-b127-0f76a4b01d38",
ConcurrencyStamp = "13ae3b09-1b07-46d7-870e-ff9a78ac556d",
Name = "Employee"
});
});
@@ -298,13 +297,13 @@ namespace Data.Migrations
b.HasData(
new
{
UserId = "28c75ddc-f972-4532-be46-adae03df32fb",
RoleId = "14ca6e11-5721-43c8-a170-b74dbd8dd7f8"
UserId = "691a45ed-4399-47a1-a8c2-2a2abac9ba74",
RoleId = "8af8225c-ac26-406b-9d29-c172f95240c0"
},
new
{
UserId = "28c75ddc-f972-4532-be46-adae03df32fb",
RoleId = "b79e8165-7e4a-409f-8d27-455dd03ca398"
UserId = "691a45ed-4399-47a1-a8c2-2a2abac9ba74",
RoleId = "b006be23-eb9f-480d-b127-0f76a4b01d38"
});
});
@@ -337,9 +336,7 @@ namespace Data.Migrations
b.HasOne("Data.Entities.User", "User")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
.HasForeignKey("UserId");
b.Navigation("Car");

View File

@@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Data.Migrations
{
public partial class initial : Migration
public partial class Initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
@@ -182,7 +182,7 @@ namespace Data.Migrations
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: false)
UserId = table.Column<string>(type: "nvarchar(450)", nullable: true)
},
constraints: table =>
{
@@ -192,7 +192,7 @@ namespace Data.Migrations
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Rents_Cars_CarId",
column: x => x.CarId,
@@ -204,27 +204,27 @@ namespace Data.Migrations
migrationBuilder.InsertData(
table: "AspNetRoles",
columns: new[] { "Id", "ConcurrencyStamp", "Name", "NormalizedName" },
values: new object[] { "14ca6e11-5721-43c8-a170-b74dbd8dd7f8", "18c88b14-89fd-4563-927c-23ed1da3d868", "Admin", null });
values: new object[] { "8af8225c-ac26-406b-9d29-c172f95240c0", "446df763-47b2-4b18-b196-86f63f2a6a8b", "Admin", null });
migrationBuilder.InsertData(
table: "AspNetRoles",
columns: new[] { "Id", "ConcurrencyStamp", "Name", "NormalizedName" },
values: new object[] { "b79e8165-7e4a-409f-8d27-455dd03ca398", "32443d24-2ba0-4d21-90f4-40971854864c", "Employee", null });
values: new object[] { "b006be23-eb9f-480d-b127-0f76a4b01d38", "13ae3b09-1b07-46d7-870e-ff9a78ac556d", "Employee", null });
migrationBuilder.InsertData(
table: "AspNetUsers",
columns: new[] { "Id", "AccessFailedCount", "ConcurrencyStamp", "Email", "EmailConfirmed", "FirstName", "LastName", "LockoutEnabled", "LockoutEnd", "NormalizedEmail", "NormalizedUserName", "PasswordHash", "PersonalNumber", "PhoneNumber", "PhoneNumberConfirmed", "SecurityStamp", "TwoFactorEnabled", "UserName" },
values: new object[] { "28c75ddc-f972-4532-be46-adae03df32fb", 0, "33c93ad2-8f61-4e3c-8d61-1583ea02a347", "admin@admin.admin", false, null, null, false, null, null, null, "AQAAAAEAACcQAAAAEMzxMVddhY+nvlkkmN6QRcuyj3Vh4iVvZ9xMarXBCh1APRAhWUU90mP4SOf0qS3A8Q==", null, null, false, "f548cc6f-ef08-4714-9c7a-e4e660cdced3", false, "admin" });
values: new object[] { "691a45ed-4399-47a1-a8c2-2a2abac9ba74", 0, "c91c153e-ba2e-4da9-a397-5ac6f098d369", "admin@admin.admin", false, null, null, false, null, null, null, "AQAAAAEAACcQAAAAEBajAJZYLZCUESCrgsH15V9n0OFqo9MLHWVzA3zHo7aqKbmSoUCv1TAu9j3r+PC/mw==", null, null, false, "b105af8c-ee72-4969-9ba5-98bd13897459", false, "admin" });
migrationBuilder.InsertData(
table: "AspNetUserRoles",
columns: new[] { "RoleId", "UserId" },
values: new object[] { "14ca6e11-5721-43c8-a170-b74dbd8dd7f8", "28c75ddc-f972-4532-be46-adae03df32fb" });
values: new object[] { "8af8225c-ac26-406b-9d29-c172f95240c0", "691a45ed-4399-47a1-a8c2-2a2abac9ba74" });
migrationBuilder.InsertData(
table: "AspNetUserRoles",
columns: new[] { "RoleId", "UserId" },
values: new object[] { "b79e8165-7e4a-409f-8d27-455dd03ca398", "28c75ddc-f972-4532-be46-adae03df32fb" });
values: new object[] { "b006be23-eb9f-480d-b127-0f76a4b01d38", "691a45ed-4399-47a1-a8c2-2a2abac9ba74" });
migrationBuilder.CreateIndex(
name: "IX_AspNetRoleClaims_RoleId",

View File

@@ -66,7 +66,6 @@ namespace Data.Migrations
.HasColumnType("datetime2");
b.Property<string>("UserId")
.IsRequired()
.HasColumnType("nvarchar(450)");
b.HasKey("Id");
@@ -154,15 +153,15 @@ namespace Data.Migrations
b.HasData(
new
{
Id = "28c75ddc-f972-4532-be46-adae03df32fb",
Id = "691a45ed-4399-47a1-a8c2-2a2abac9ba74",
AccessFailedCount = 0,
ConcurrencyStamp = "33c93ad2-8f61-4e3c-8d61-1583ea02a347",
ConcurrencyStamp = "c91c153e-ba2e-4da9-a397-5ac6f098d369",
Email = "admin@admin.admin",
EmailConfirmed = false,
LockoutEnabled = false,
PasswordHash = "AQAAAAEAACcQAAAAEMzxMVddhY+nvlkkmN6QRcuyj3Vh4iVvZ9xMarXBCh1APRAhWUU90mP4SOf0qS3A8Q==",
PasswordHash = "AQAAAAEAACcQAAAAEBajAJZYLZCUESCrgsH15V9n0OFqo9MLHWVzA3zHo7aqKbmSoUCv1TAu9j3r+PC/mw==",
PhoneNumberConfirmed = false,
SecurityStamp = "f548cc6f-ef08-4714-9c7a-e4e660cdced3",
SecurityStamp = "b105af8c-ee72-4969-9ba5-98bd13897459",
TwoFactorEnabled = false,
UserName = "admin"
});
@@ -197,14 +196,14 @@ namespace Data.Migrations
b.HasData(
new
{
Id = "14ca6e11-5721-43c8-a170-b74dbd8dd7f8",
ConcurrencyStamp = "18c88b14-89fd-4563-927c-23ed1da3d868",
Id = "8af8225c-ac26-406b-9d29-c172f95240c0",
ConcurrencyStamp = "446df763-47b2-4b18-b196-86f63f2a6a8b",
Name = "Admin"
},
new
{
Id = "b79e8165-7e4a-409f-8d27-455dd03ca398",
ConcurrencyStamp = "32443d24-2ba0-4d21-90f4-40971854864c",
Id = "b006be23-eb9f-480d-b127-0f76a4b01d38",
ConcurrencyStamp = "13ae3b09-1b07-46d7-870e-ff9a78ac556d",
Name = "Employee"
});
});
@@ -296,13 +295,13 @@ namespace Data.Migrations
b.HasData(
new
{
UserId = "28c75ddc-f972-4532-be46-adae03df32fb",
RoleId = "14ca6e11-5721-43c8-a170-b74dbd8dd7f8"
UserId = "691a45ed-4399-47a1-a8c2-2a2abac9ba74",
RoleId = "8af8225c-ac26-406b-9d29-c172f95240c0"
},
new
{
UserId = "28c75ddc-f972-4532-be46-adae03df32fb",
RoleId = "b79e8165-7e4a-409f-8d27-455dd03ca398"
UserId = "691a45ed-4399-47a1-a8c2-2a2abac9ba74",
RoleId = "b006be23-eb9f-480d-b127-0f76a4b01d38"
});
});
@@ -335,9 +334,7 @@ namespace Data.Migrations
b.HasOne("Data.Entities.User", "User")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
.HasForeignKey("UserId");
b.Navigation("Car");

View File

@@ -11,13 +11,15 @@ namespace Data.Entities
{
[Required]
[Key]
[Display(Name = "Car ID")]
public int Id { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public int Year { get; set; }
[Display(Name = "Seats")]
public int CountPassengerSeats { get; set; }
public string Description { get; set; }
[Display(Name = "Daily price")]
public decimal PriceForDay { get; set; }
}
}

View File

@@ -12,19 +12,19 @@ namespace Data.Entities
public class Rents
{
public int Id { get; set; }
[Required]
[ForeignKey("CarId")]
public Car Car { get; set; }
public int CarId { get; set; }
public virtual Car Car { get; set; }
[Required]
[Display(Name = "Start Date")]
public DateTime StartDate { get; set; }
[Required]
[Display(Name = "End Date")]
public DateTime EndDate { get; set; }
[Required]
[ForeignKey("UserId")]
public User User { get; set; }
public string UserId { get; set; }
public virtual User User { get; set; }
}
}

View File

@@ -11,10 +11,11 @@ namespace Data.Entities
{
public class User : IdentityUser<string>
{
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "ID Number")]
public string PersonalNumber { get; set; }
}

View File

@@ -61,6 +61,7 @@ namespace WebApp.Controllers
[Authorize]
public async Task<IActionResult> Create(Rents rents)
{
if (ModelState.IsValid)
{
var car = _context.Cars.FirstOrDefault(car => car.Id == 1);

View File

@@ -29,7 +29,7 @@ namespace API
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<RentACarDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddIdentity<User, IdentityRole>(options =>
{
@@ -45,7 +45,6 @@ namespace API
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<RentACarDbContext>();
services.AddControllersWithViews();
services.AddRazorPages();
}

View File

@@ -15,7 +15,7 @@
<thead>
<tr>
<th>
ID
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Brand)
@@ -27,13 +27,13 @@
@Html.DisplayNameFor(model => model.Year)
</th>
<th>
Seats
@Html.DisplayNameFor(model => model.CountPassengerSeats)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
Daily price
@Html.DisplayNameFor(model => model.PriceForDay)
</th>
@if (this.User.IsInRole("Admin"))
{

View File

@@ -9,18 +9,72 @@
<div>
<hr />
<dl class="row">
<dt class = "col-sm-2">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Car.Id)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Car.Id)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Car.Brand)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Car.Brand)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Car.Model)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Car.Model)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Car.CountPassengerSeats)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Car.CountPassengerSeats)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Car.PriceForDay)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Car.PriceForDay)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.StartDate)
</dt>
<dd class = "col-sm-10">
<dd class="col-sm-10">
@Html.DisplayFor(model => model.StartDate)
</dd>
<dt class = "col-sm-2">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.EndDate)
</dt>
<dd class = "col-sm-10">
<dd class="col-sm-10">
@Html.DisplayFor(model => model.EndDate)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.FirstName)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.FirstName)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.LastName)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.LastName)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.UserName)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.UserName)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.Email)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.Email)
</dd>
</dl>
</div>
<div>

View File

@@ -15,16 +15,16 @@
<thead>
<tr>
<th>
Car ID
@Html.DisplayNameFor(model => model.Car.Id)
</th>
<th>
Start Date
@Html.DisplayNameFor(model => model.StartDate)
</th>
<th>
End Date
@Html.DisplayNameFor(model => model.EndDate)
</th>
<th>
User
@Html.DisplayNameFor(model => model.User.UserName)
</th>
@if (this.User.IsInRole("Admin"))
{
@@ -37,28 +37,28 @@
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Car.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.StartDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.EndDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.UserName)
</td>
@if (this.User.IsInRole("Admin"))
{
<tr>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
@Html.DisplayFor(modelItem => item.Car.Id)
</td>
}
</tr>
<td>
@Html.DisplayFor(modelItem => item.StartDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.EndDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.UserName)
</td>
@if (this.User.IsInRole("Admin"))
{
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
}
</tr>
}
</tbody>
</table>

View File

@@ -13,22 +13,22 @@
<thead>
<tr>
<th>
First name
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
Last name
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
ID number
@Html.DisplayNameFor(model => model.PersonalNumber)
</th>
<th>
Username
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
E-mail
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
Phone number
@Html.DisplayNameFor(model => model.PhoneNumber)
</th>
<th>
Actions