ef relation

main
syneffort 2 years ago
parent 428733668d
commit 0c431ce57f
  1. 18
      MySolution/InventoryManagement/Components/Accessary.cs
  2. 1
      MySolution/InventoryManagement/Components/InventoryContext.cs
  3. 38
      MySolution/InventoryManagement/Components/InventoryManager.cs
  4. 2
      MySolution/InventoryManagement/Components/Product.cs
  5. 91
      MySolution/InventoryManagement/Migrations/20231017084526_accessary.Designer.cs
  6. 47
      MySolution/InventoryManagement/Migrations/20231017084526_accessary.cs
  7. 93
      MySolution/InventoryManagement/Migrations/20231018015608_Acc-Prod.Designer.cs
  8. 22
      MySolution/InventoryManagement/Migrations/20231018015608_Acc-Prod.cs
  9. 41
      MySolution/InventoryManagement/Migrations/InventoryContextModelSnapshot.cs
  10. 14
      MySolution/InventoryManagement/Program.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; }
}
}

@ -10,6 +10,7 @@ namespace InventoryManagement.Components
internal class InventoryContext : DbContext internal class InventoryContext : DbContext
{ {
public DbSet<Product> Products { get; set; } public DbSet<Product> Products { get; set; }
public DbSet<Accessary> Accessaries { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {

@ -1,4 +1,5 @@
using System; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; 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() public void ShowInventory()
{ {
using (var context = new InventoryContext()) using (var context = new InventoryContext())
{ {
Console.WriteLine("********** Current Inventory **********"); Console.WriteLine("********** Current Inventory **********");
Console.WriteLine("***** Products *****");
foreach (var product in context.Products) foreach (var product in context.Products)
{ {
Console.WriteLine($"ID: {product.Id}, Name: {product.Name}, Quantity: {product.Quantity}"); 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("****************************************"); Console.WriteLine("****************************************");
} }
} }

@ -15,5 +15,7 @@ namespace InventoryManagement.Components
public int Id { get; } public int Id { get; }
public string Name { get; set; } public string Name { get; set; }
public int Quantity { get; set; } public int Quantity { get; set; }
public ICollection<Accessary> CompatibleAccessaries { get; set; }
} }
} }

@ -0,0 +1,91 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Description")
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("ProductId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ProductId");
b.ToTable("Accessaries");
});
modelBuilder.Entity("InventoryManagement.Components.Product", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(450)");
b.Property<int>("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
}
}
}

@ -0,0 +1,47 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace InventoryManagement.Migrations
{
/// <inheritdoc />
public partial class accessary : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Accessaries",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ProductId = table.Column<int>(type: "int", nullable: false),
Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
Description = table.Column<string>(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");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Accessaries");
}
}
}

@ -0,0 +1,93 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Description")
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("ProductId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ProductId");
b.ToTable("Accessaries");
});
modelBuilder.Entity("InventoryManagement.Components.Product", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(450)");
b.Property<int>("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
}
}
}

@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace InventoryManagement.Migrations
{
/// <inheritdoc />
public partial class AccProd : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

@ -21,6 +21,31 @@ namespace InventoryManagement.Migrations
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("InventoryManagement.Components.Accessary", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Description")
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("ProductId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ProductId");
b.ToTable("Accessaries");
});
modelBuilder.Entity("InventoryManagement.Components.Product", b => modelBuilder.Entity("InventoryManagement.Components.Product", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@ -43,6 +68,22 @@ namespace InventoryManagement.Migrations
b.ToTable("Products"); 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 #pragma warning restore 612, 618
} }
} }

@ -16,7 +16,7 @@ namespace InventoryManagement
{ {
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Pease Enter the command."); 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> "); Console.Write("CMD> ");
string cmd = Console.ReadLine().ToLower(); string cmd = Console.ReadLine().ToLower();
switch (cmd) switch (cmd)
@ -81,6 +81,18 @@ namespace InventoryManagement
InventoryManager.SellProduct(name, quantity); InventoryManager.SellProduct(name, quantity);
break; 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;
}
} }
} }

Loading…
Cancel
Save