static void Main(string[] args) { using (var db = new ProdContext()) { //Console.Write("Write a name of a category: "); //string name = Console.ReadLine(); //AddCategory(name, db); //ShowAllCategoriesQuery(db); //ShowAllCategoriesMethod(db); var entity = db.Products.Find(1); db.Entry(entity).Property("UnitsInStock").CurrentValue = 10; entity = db.Products.Find(2); db.Entry(entity).Property("UnitsInStock").CurrentValue = 2; entity = db.Products.Find(3); db.Entry(entity).Property("UnitsInStock").CurrentValue = 5; db.SaveChanges(); ShowAllCategoriesAndProducts showAll = new ShowAllCategoriesAndProducts(db); showAll.ShowAllCategoriesAndProductsEagerLoading(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); var categoryForm = new CategoryForm(); categoryForm.ShowDialog(); } }
private void AddToBasket_Click(object sender, EventArgs e) { if (dataGridView2.SelectedRows.Count != 0) { string productName, productID; decimal productPrice, currentPrice; int productUnits; for (int i = 0; i < dataGridView2.SelectedRows.Count; i++) { productName = dataGridView2.SelectedRows[i].Cells[1].Value.ToString(); productID = dataGridView2.SelectedRows[i].Cells[0].Value.ToString(); productUnits = Convert.ToInt32(dataGridView2.SelectedRows[i].Cells[2].Value); if (productUnits == 0) { Debug.WriteLine($"{productName}: No units in stock."); continue; } productPrice = Convert.ToDecimal(dataGridView2.SelectedRows[i].Cells[4].Value); basket.Items.Add($"{productID} {productName}"); currentPrice = Convert.ToDecimal(price.Text.ToString()) + productPrice; price.Text = currentPrice.ToString(); productUnits--; var entity = pContext.Products.Find(Convert.ToInt32(productID)); pContext.Entry(entity).Property("UnitsInStock").CurrentValue = productUnits; pContext.SaveChanges(); pContext.Products.Load(); this.dataGridView2.Refresh(); } } }