From 0c431ce57ffcceba5783f6678ec2b05f24248804 Mon Sep 17 00:00:00 2001 From: syneffort Date: Wed, 18 Oct 2023 11:02:10 +0900 Subject: [PATCH] ef relation --- .../Components/Accessary.cs | 18 ++++ .../Components/InventoryContext.cs | 1 + .../Components/InventoryManager.cs | 38 +++++++- .../InventoryManagement/Components/Product.cs | 2 + .../20231017084526_accessary.Designer.cs | 91 ++++++++++++++++++ .../Migrations/20231017084526_accessary.cs | 47 ++++++++++ .../20231018015608_Acc-Prod.Designer.cs | 93 +++++++++++++++++++ .../Migrations/20231018015608_Acc-Prod.cs | 22 +++++ .../InventoryContextModelSnapshot.cs | 41 ++++++++ MySolution/InventoryManagement/Program.cs | 14 ++- 10 files changed, 365 insertions(+), 2 deletions(-) create mode 100644 MySolution/InventoryManagement/Components/Accessary.cs create mode 100644 MySolution/InventoryManagement/Migrations/20231017084526_accessary.Designer.cs create mode 100644 MySolution/InventoryManagement/Migrations/20231017084526_accessary.cs create mode 100644 MySolution/InventoryManagement/Migrations/20231018015608_Acc-Prod.Designer.cs create mode 100644 MySolution/InventoryManagement/Migrations/20231018015608_Acc-Prod.cs diff --git a/MySolution/InventoryManagement/Components/Accessary.cs b/MySolution/InventoryManagement/Components/Accessary.cs new file mode 100644 index 0000000..6bfca38 --- /dev/null +++ b/MySolution/InventoryManagement/Components/Accessary.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InventoryManagement.Components +{ + internal class Accessary + { + public int Id { get; set; } + public int ProductId { get; set; } + public string Name { get; set; } + public string? Description { get; set; } + + public Product CompatibleProduct { get; set; } + } +} diff --git a/MySolution/InventoryManagement/Components/InventoryContext.cs b/MySolution/InventoryManagement/Components/InventoryContext.cs index 1cd06d9..09055db 100644 --- a/MySolution/InventoryManagement/Components/InventoryContext.cs +++ b/MySolution/InventoryManagement/Components/InventoryContext.cs @@ -10,6 +10,7 @@ namespace InventoryManagement.Components internal class InventoryContext : DbContext { public DbSet Products { get; set; } + public DbSet Accessaries { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { diff --git a/MySolution/InventoryManagement/Components/InventoryManager.cs b/MySolution/InventoryManagement/Components/InventoryManager.cs index 0b5b43d..21e62e6 100644 --- a/MySolution/InventoryManagement/Components/InventoryManager.cs +++ b/MySolution/InventoryManagement/Components/InventoryManager.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -106,15 +107,50 @@ namespace InventoryManagement.Components } } + public void AddAccessary(int productId, string name, string description) + { + using (var context = new InventoryContext()) + { + var product = context.Products.Find(productId); + if (product == null) + { + Console.WriteLine("Product not found."); + return; + } + + context.Entry(product).Collection(e => e.CompatibleAccessaries).Load(); + + Accessary acc = new Accessary() + { + ProductId = productId, + Name = name, + Description = description, + }; + + product.CompatibleAccessaries.Add(acc); + context.SaveChanges(); + Console.WriteLine($"{acc.Name} added."); + } + } + public void ShowInventory() { using (var context = new InventoryContext()) { Console.WriteLine("********** Current Inventory **********"); + Console.WriteLine("***** Products *****"); foreach (var product in context.Products) { Console.WriteLine($"ID: {product.Id}, Name: {product.Name}, Quantity: {product.Quantity}"); } + Console.WriteLine("********************"); + Console.WriteLine(); + Console.WriteLine("***** Accesarry *****"); + foreach (var accesarry in context.Accessaries.Include(e => e.CompatibleProduct)) + { + Console.WriteLine($"ID: {accesarry.Id}, Name: {accesarry.Name}, Compatible product: {accesarry.CompatibleProduct.Name}"); + } + Console.WriteLine("*********************"); Console.WriteLine("****************************************"); } } diff --git a/MySolution/InventoryManagement/Components/Product.cs b/MySolution/InventoryManagement/Components/Product.cs index bedf71f..811eaff 100644 --- a/MySolution/InventoryManagement/Components/Product.cs +++ b/MySolution/InventoryManagement/Components/Product.cs @@ -15,5 +15,7 @@ namespace InventoryManagement.Components public int Id { get; } public string Name { get; set; } public int Quantity { get; set; } + + public ICollection CompatibleAccessaries { get; set; } } } diff --git a/MySolution/InventoryManagement/Migrations/20231017084526_accessary.Designer.cs b/MySolution/InventoryManagement/Migrations/20231017084526_accessary.Designer.cs new file mode 100644 index 0000000..23430f5 --- /dev/null +++ b/MySolution/InventoryManagement/Migrations/20231017084526_accessary.Designer.cs @@ -0,0 +1,91 @@ +// +using InventoryManagement.Components; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace InventoryManagement.Migrations +{ + [DbContext(typeof(InventoryContext))] + [Migration("20231017084526_accessary")] + partial class accessary + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("InventoryManagement.Components.Accessary", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.ToTable("Accessaries"); + }); + + modelBuilder.Entity("InventoryManagement.Components.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("Quantity") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("InventoryManagement.Components.Accessary", b => + { + b.HasOne("InventoryManagement.Components.Product", null) + .WithMany("CompatibleAccessaries") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("InventoryManagement.Components.Product", b => + { + b.Navigation("CompatibleAccessaries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/InventoryManagement/Migrations/20231017084526_accessary.cs b/MySolution/InventoryManagement/Migrations/20231017084526_accessary.cs new file mode 100644 index 0000000..66324ff --- /dev/null +++ b/MySolution/InventoryManagement/Migrations/20231017084526_accessary.cs @@ -0,0 +1,47 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace InventoryManagement.Migrations +{ + /// + public partial class accessary : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Accessaries", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ProductId = table.Column(type: "int", nullable: false), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Description = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Accessaries", x => x.Id); + table.ForeignKey( + name: "FK_Accessaries_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Accessaries_ProductId", + table: "Accessaries", + column: "ProductId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Accessaries"); + } + } +} diff --git a/MySolution/InventoryManagement/Migrations/20231018015608_Acc-Prod.Designer.cs b/MySolution/InventoryManagement/Migrations/20231018015608_Acc-Prod.Designer.cs new file mode 100644 index 0000000..b00ee72 --- /dev/null +++ b/MySolution/InventoryManagement/Migrations/20231018015608_Acc-Prod.Designer.cs @@ -0,0 +1,93 @@ +// +using InventoryManagement.Components; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace InventoryManagement.Migrations +{ + [DbContext(typeof(InventoryContext))] + [Migration("20231018015608_Acc-Prod")] + partial class AccProd + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("InventoryManagement.Components.Accessary", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.ToTable("Accessaries"); + }); + + modelBuilder.Entity("InventoryManagement.Components.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("Quantity") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("InventoryManagement.Components.Accessary", b => + { + b.HasOne("InventoryManagement.Components.Product", "CompatibleProduct") + .WithMany("CompatibleAccessaries") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("CompatibleProduct"); + }); + + modelBuilder.Entity("InventoryManagement.Components.Product", b => + { + b.Navigation("CompatibleAccessaries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/InventoryManagement/Migrations/20231018015608_Acc-Prod.cs b/MySolution/InventoryManagement/Migrations/20231018015608_Acc-Prod.cs new file mode 100644 index 0000000..260d5aa --- /dev/null +++ b/MySolution/InventoryManagement/Migrations/20231018015608_Acc-Prod.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace InventoryManagement.Migrations +{ + /// + public partial class AccProd : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/MySolution/InventoryManagement/Migrations/InventoryContextModelSnapshot.cs b/MySolution/InventoryManagement/Migrations/InventoryContextModelSnapshot.cs index 63f8285..f3cd2df 100644 --- a/MySolution/InventoryManagement/Migrations/InventoryContextModelSnapshot.cs +++ b/MySolution/InventoryManagement/Migrations/InventoryContextModelSnapshot.cs @@ -21,6 +21,31 @@ namespace InventoryManagement.Migrations SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + modelBuilder.Entity("InventoryManagement.Components.Accessary", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.ToTable("Accessaries"); + }); + modelBuilder.Entity("InventoryManagement.Components.Product", b => { b.Property("Id") @@ -43,6 +68,22 @@ namespace InventoryManagement.Migrations b.ToTable("Products"); }); + + modelBuilder.Entity("InventoryManagement.Components.Accessary", b => + { + b.HasOne("InventoryManagement.Components.Product", "CompatibleProduct") + .WithMany("CompatibleAccessaries") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("CompatibleProduct"); + }); + + modelBuilder.Entity("InventoryManagement.Components.Product", b => + { + b.Navigation("CompatibleAccessaries"); + }); #pragma warning restore 612, 618 } } diff --git a/MySolution/InventoryManagement/Program.cs b/MySolution/InventoryManagement/Program.cs index 89cb6b1..755df90 100644 --- a/MySolution/InventoryManagement/Program.cs +++ b/MySolution/InventoryManagement/Program.cs @@ -16,7 +16,7 @@ namespace InventoryManagement { Console.WriteLine(); Console.WriteLine("Pease Enter the command."); - Console.WriteLine("(show: Show current Inventory, add: Add new product, buy: Buy product, sell: Sell product)"); + Console.WriteLine("(show: Show current Inventory, add: Add new product, buy: Buy product, sell: Sell product, acc: Add accessary)"); Console.Write("CMD> "); string cmd = Console.ReadLine().ToLower(); switch (cmd) @@ -81,6 +81,18 @@ namespace InventoryManagement InventoryManager.SellProduct(name, quantity); break; } + case "acc": + { + Console.Write("\tProduct id: "); + string productId = Console.ReadLine(); + int pId = int.Parse(productId); + Console.Write("\tAccessary name: "); + string name = Console.ReadLine(); + Console.Write("\tAccessary description: "); + string desc = Console.ReadLine(); + InventoryManager.AddAccessary(pId, name, desc); + break; + } } }