Relation and models fixes.

This commit is contained in:
Yuee Shiness 2023-05-17 18:34:28 +04:00
parent 8a4b8a5f79
commit f64c7b284c
8 changed files with 635 additions and 25 deletions

View File

@ -14,7 +14,7 @@ namespace ComputerStoreDatabaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Initial Catalog=ComputerStoreDatabase;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=True");
optionsBuilder.UseSqlServer(@"Data Source=YUEEJKE\SQLEXPRESS;Initial Catalog=ComputerStoreDatabase;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}

View File

@ -0,0 +1,496 @@
// <auto-generated />
using System;
using ComputerStoreDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace ComputerStoreDatabaseImplement.Migrations
{
[DbContext(typeof(ComputerStoreDatabase))]
[Migration("20230517142400_RelationFixes")]
partial class RelationFixes
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Component", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("ID");
b.ToTable("Components");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Consignment", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<int>("OrderID")
.HasColumnType("int");
b.Property<double>("Price")
.HasColumnType("float");
b.Property<int?>("ProductID")
.HasColumnType("int");
b.HasKey("ID");
b.HasIndex("OrderID");
b.HasIndex("ProductID");
b.ToTable("Consignments");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.ConsignmentProduct", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<int>("ConsignmentID")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ProductID")
.HasColumnType("int");
b.HasKey("ID");
b.HasIndex("ConsignmentID");
b.HasIndex("ProductID");
b.ToTable("ConsignmentProducts");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Employee", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<string>("FirstName")
.HasColumnType("nvarchar(max)");
b.Property<string>("LastName")
.HasColumnType("nvarchar(max)");
b.Property<string>("MiddleName")
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("ID");
b.ToTable("Employees");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Order", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<double>("Price")
.HasColumnType("float");
b.Property<int>("SellerID")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.HasKey("ID");
b.HasIndex("SellerID");
b.ToTable("Orders");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.PC", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<int>("EmployeeID")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.Property<int>("RequestID")
.HasColumnType("int");
b.HasKey("ID");
b.HasIndex("EmployeeID");
b.ToTable("PCs");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Product", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<int>("EmployeeID")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("ID");
b.HasIndex("EmployeeID");
b.ToTable("Products");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.ProductComponent", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<int>("ComponentID")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ProductID")
.HasColumnType("int");
b.HasKey("ID");
b.HasIndex("ComponentID");
b.HasIndex("ProductID");
b.ToTable("ProductComponents");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Request", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<int>("OrderID")
.HasColumnType("int");
b.Property<int?>("PCID")
.HasColumnType("int");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("ID");
b.HasIndex("OrderID");
b.ToTable("Requests");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.RequestComponent", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<int>("ComponentID")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int?>("PCID")
.HasColumnType("int");
b.Property<int>("RequestID")
.HasColumnType("int");
b.HasKey("ID");
b.HasIndex("ComponentID");
b.HasIndex("PCID");
b.HasIndex("RequestID");
b.ToTable("RequestComponents");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Seller", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<string>("FirstName")
.HasColumnType("nvarchar(max)");
b.Property<string>("LastName")
.HasColumnType("nvarchar(max)");
b.Property<string>("MiddleName")
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("ID");
b.ToTable("Sellers");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Consignment", b =>
{
b.HasOne("ComputerStoreDatabaseImplement.Models.Order", "Order")
.WithMany("_consignments")
.HasForeignKey("OrderID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ComputerStoreDatabaseImplement.Models.Product", null)
.WithMany("Consignments")
.HasForeignKey("ProductID");
b.Navigation("Order");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.ConsignmentProduct", b =>
{
b.HasOne("ComputerStoreDatabaseImplement.Models.Consignment", "Consignment")
.WithMany("Products")
.HasForeignKey("ConsignmentID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ComputerStoreDatabaseImplement.Models.Product", "Product")
.WithMany()
.HasForeignKey("ProductID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Consignment");
b.Navigation("Product");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Order", b =>
{
b.HasOne("ComputerStoreDatabaseImplement.Models.Seller", "Seller")
.WithMany("Orders")
.HasForeignKey("SellerID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Seller");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.PC", b =>
{
b.HasOne("ComputerStoreDatabaseImplement.Models.Employee", "Employee")
.WithMany("PCs")
.HasForeignKey("EmployeeID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Employee");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Product", b =>
{
b.HasOne("ComputerStoreDatabaseImplement.Models.Employee", "Employee")
.WithMany("Products")
.HasForeignKey("EmployeeID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Employee");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.ProductComponent", b =>
{
b.HasOne("ComputerStoreDatabaseImplement.Models.Component", "Component")
.WithMany("ConsignmentComponents")
.HasForeignKey("ComponentID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ComputerStoreDatabaseImplement.Models.Product", "Product")
.WithMany("Components")
.HasForeignKey("ProductID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Product");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Request", b =>
{
b.HasOne("ComputerStoreDatabaseImplement.Models.Order", "Order")
.WithMany("_requests")
.HasForeignKey("OrderID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Order");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.RequestComponent", b =>
{
b.HasOne("ComputerStoreDatabaseImplement.Models.Component", "Component")
.WithMany("RequestComponents")
.HasForeignKey("ComponentID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ComputerStoreDatabaseImplement.Models.PC", "PC")
.WithMany("Components")
.HasForeignKey("PCID");
b.HasOne("ComputerStoreDatabaseImplement.Models.Request", "Request")
.WithMany("PCs")
.HasForeignKey("RequestID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("PC");
b.Navigation("Request");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Component", b =>
{
b.Navigation("ConsignmentComponents");
b.Navigation("RequestComponents");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Consignment", b =>
{
b.Navigation("Products");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Employee", b =>
{
b.Navigation("PCs");
b.Navigation("Products");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Order", b =>
{
b.Navigation("_consignments");
b.Navigation("_requests");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.PC", b =>
{
b.Navigation("Components");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Product", b =>
{
b.Navigation("Components");
b.Navigation("Consignments");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Request", b =>
{
b.Navigation("PCs");
});
modelBuilder.Entity("ComputerStoreDatabaseImplement.Models.Seller", b =>
{
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,112 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ComputerStoreDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class RelationFixes : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Orders_Sellers_OrderID",
table: "Orders");
migrationBuilder.DropForeignKey(
name: "FK_RequestComponents_PCs_PCID",
table: "RequestComponents");
migrationBuilder.DropIndex(
name: "IX_Orders_OrderID",
table: "Orders");
migrationBuilder.DropColumn(
name: "OrderID",
table: "Orders");
migrationBuilder.AlterColumn<int>(
name: "PCID",
table: "RequestComponents",
type: "int",
nullable: true,
oldClrType: typeof(int),
oldType: "int");
migrationBuilder.CreateIndex(
name: "IX_Orders_SellerID",
table: "Orders",
column: "SellerID");
migrationBuilder.AddForeignKey(
name: "FK_Orders_Sellers_SellerID",
table: "Orders",
column: "SellerID",
principalTable: "Sellers",
principalColumn: "ID",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_RequestComponents_PCs_PCID",
table: "RequestComponents",
column: "PCID",
principalTable: "PCs",
principalColumn: "ID");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Orders_Sellers_SellerID",
table: "Orders");
migrationBuilder.DropForeignKey(
name: "FK_RequestComponents_PCs_PCID",
table: "RequestComponents");
migrationBuilder.DropIndex(
name: "IX_Orders_SellerID",
table: "Orders");
migrationBuilder.AlterColumn<int>(
name: "PCID",
table: "RequestComponents",
type: "int",
nullable: false,
defaultValue: 0,
oldClrType: typeof(int),
oldType: "int",
oldNullable: true);
migrationBuilder.AddColumn<int>(
name: "OrderID",
table: "Orders",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.CreateIndex(
name: "IX_Orders_OrderID",
table: "Orders",
column: "OrderID");
migrationBuilder.AddForeignKey(
name: "FK_Orders_Sellers_OrderID",
table: "Orders",
column: "OrderID",
principalTable: "Sellers",
principalColumn: "ID",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_RequestComponents_PCs_PCID",
table: "RequestComponents",
column: "PCID",
principalTable: "PCs",
principalColumn: "ID",
onDelete: ReferentialAction.Cascade);
}
}
}

View File

@ -138,9 +138,6 @@ namespace ComputerStoreDatabaseImplement.Migrations
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("OrderID")
.HasColumnType("int");
b.Property<double>("Price")
.HasColumnType("float");
@ -152,7 +149,7 @@ namespace ComputerStoreDatabaseImplement.Migrations
b.HasKey("ID");
b.HasIndex("OrderID");
b.HasIndex("SellerID");
b.ToTable("Orders");
});
@ -274,7 +271,7 @@ namespace ComputerStoreDatabaseImplement.Migrations
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PCID")
b.Property<int?>("PCID")
.HasColumnType("int");
b.Property<int>("RequestID")
@ -359,7 +356,7 @@ namespace ComputerStoreDatabaseImplement.Migrations
{
b.HasOne("ComputerStoreDatabaseImplement.Models.Seller", "Seller")
.WithMany("Orders")
.HasForeignKey("OrderID")
.HasForeignKey("SellerID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
@ -428,9 +425,7 @@ namespace ComputerStoreDatabaseImplement.Migrations
b.HasOne("ComputerStoreDatabaseImplement.Models.PC", "PC")
.WithMany("Components")
.HasForeignKey("PCID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
.HasForeignKey("PCID");
b.HasOne("ComputerStoreDatabaseImplement.Models.Request", "Request")
.WithMany("PCs")

View File

@ -87,12 +87,16 @@ namespace ComputerStoreDatabaseImplement.Models
var pcComponents = context.RequestComponents.Where(rec => rec.PCID == model.ID && rec.RequestID == model.RequestID).ToList();
if(pcComponents != null && pcComponents.Count > 0)
{
context.RequestComponents.RemoveRange(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID));
context.SaveChanges();
if(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID).Any())
{
context.RequestComponents.RemoveRange(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID));
context.SaveChanges();
}
foreach(var updateComponent in pcComponents)
{
updateComponent.Count = model.PCComponents[updateComponent.PCID].Item2;
model.PCComponents.Remove(updateComponent.PCID);
updateComponent.Count = model.PCComponents[updateComponent.ComponentID].Item2;
model.PCComponents.Remove(updateComponent.ComponentID);
}
context.SaveChanges();
}

View File

@ -83,20 +83,23 @@ namespace ComputerStoreDatabaseImplement.Models
public void UpdateComponents(ComputerStoreDatabase context, ProductBindingModel model)
{
var productComponents = context.ProductComponents.Where(rec => rec.ProductID == model.ID).ToList();
if(productComponents != null && productComponents.Count > 0)
if (productComponents != null && productComponents.Count > 0)
{
context.ProductComponents.RemoveRange(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentID)));
context.SaveChanges();
foreach(var updateComponent in productComponents)
if (productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentID)).Any())
{
updateComponent.Count = model.ProductComponents[updateComponent.ID].Item2;
model.ProductComponents.Remove(updateComponent.ID);
context.ProductComponents.RemoveRange(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentID)));
context.SaveChanges();
}
foreach (var updateComponent in productComponents.Where(x => model.ProductComponents.ContainsKey(x.ComponentID)))
{
updateComponent.Count = model.ProductComponents[updateComponent.ComponentID].Item2;
model.ProductComponents.Remove(updateComponent.ComponentID);
}
context.SaveChanges();
}
var product = context.Products.First(x => x.ID == ID);
foreach(var pc in model.ProductComponents)
foreach (var pc in model.ProductComponents)
{
context.ProductComponents.Add(new ProductComponent
{

View File

@ -17,13 +17,13 @@ namespace ComputerStoreDatabaseImplement.Models
[Required]
public int ComponentID { get; set; }
[Required]
public int PCID { get; set; }
public int? PCID { get; set; }
[Required]
public int Count { get; set; }
public virtual PC PC { get; set; } = new();
public virtual PC? PC { get; set; } = new();
public virtual Component Component { get; set; } = new();
public virtual Request Request { get; set; } = new();
}

View File

@ -23,7 +23,7 @@ namespace ComputerStoreDatabaseImplement.Models
public string? LastName { get; private set; } = string.Empty;
public string? MiddleName { get; private set; } = string.Empty;
[ForeignKey("OrderID")]
[ForeignKey("SellerID")]
public virtual List<Order> Orders { get; set; } = new();
public static Seller? Create(SellerBindingModel? model)