public DeleteCat() { var db = new NorthwindContext(); Console.Clear(); Console.WriteLine("NORTHWIND Category & Products - Delete Category and related Products\n"); Console.WriteLine("Select the category you want to Delete:"); try { id = int.Parse(DisplayCat.DispCatSel()); // display the list of categories Console.Clear(); Console.WriteLine("NORTHWIND Category & Products - Delete Category and related Products\n"); logger.Info($"CategoryId {id} selected"); Category category = db.Categories.FirstOrDefault(c => c.CategoryId == id); Console.WriteLine($"You chose {category.CategoryName} - {category.Description}"); GetProductCount getProductCount = new GetProductCount(id); // display the product count first foreach (Product p in category.Products) // products enumerated in the category bcause of the list { Console.WriteLine($"\t{p.ProductName}"); } Console.WriteLine("Delete the Category and any related Products? Y or any key to bypass"); string del = Console.ReadLine(); logger.Info($"{del}"); if (del.ToUpper() == "Y") { var dquery = db.Products.Where(p => p.CategoryId == id); foreach (var item in dquery) { db.Products.Remove(item); db.SaveChanges(); } var delcat = db.Categories.FirstOrDefault(c => c.CategoryId == id); db.Categories.Remove(delcat); db.SaveChanges(); } } catch { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("** No Category selected"); Console.ResetColor(); } }
public DisplayCatProd() { var db = new NorthwindContext(); Console.Clear(); Console.WriteLine(); Console.WriteLine("NORTHWIND Category & Products - Display Category and related Products\n"); try { int id = int.Parse(DisplayCat.DispCatSel()); // display the list of categories Console.Clear(); Console.WriteLine("NORTHWIND Category & Products - Display Category and related Products\n"); logger.Info($"CategoryId {id} selected"); Category category = db.Categories.FirstOrDefault(c => c.CategoryId == id); Console.WriteLine($"You chose {category.CategoryName} - {category.Description}"); GetProductCount getProductCount = new GetProductCount(id); // display the product count first foreach (Product p in category.Products) // products enumerated in the category bcause of the list { if (!p.Discontinued) { Console.WriteLine($"\t{p.ProductName}"); } } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } catch { logger.Info("Error in Selection"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"** Error - Try again"); Console.ResetColor(); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } }
public DisplayAllCatProd() { int qcount = 0; int m = 0; string qsel = "Y"; var db = new NorthwindContext(); var query = db.Categories.Include("Products").OrderBy(p => p.CategoryId); // dont do .ToList() after because we can add to it with where etc. qcount = query.Count(); Console.Clear(); Console.WriteLine("NORTHWIND Category & Products - Display Category and related Products\n"); foreach (var item in query) { Console.WriteLine($"\n{item.CategoryName}:"); GetProductCount getProductCount = new GetProductCount(item.CategoryId); // display the product count first foreach (Product p in item.Products) // batches of 3 { if (!p.Discontinued) { Console.WriteLine($"\t{p.ProductName}"); } } m++; if (m == 3) { Console.WriteLine("\n Enter to continue ....."); qsel = Console.ReadLine().ToUpper(); if (qsel == "Y" || qsel != "Y") { m = 0; } } } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }