ef relation
This commit is contained in:
@@ -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("****************************************");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user