/// <summary> /// Add new Order /// </summary> /// <param name="idB">book id</param> /// <param name="idCust">customer id</param> public static void AddOrder(int idB, int idCust) { using (BookstoreEntities db = new BookstoreEntities()) { Book book = db.Books.FirstOrDefault(x => x.id == idB); Customer customer = db.Customers.FirstOrDefault(x => x.id == idCust); if (customer != null & book != null) { db.Orders.Add(new Order { idBook = idB, idCustomer = idCust }); db.SaveChanges(); } else { Console.WriteLine("Error"); } } }
/// <summary> /// Delete customer with all his orders /// </summary> /// <param name="_id">customer id</param> public static void DeleteCustomer(int _id) { using (BookstoreEntities db = new BookstoreEntities()) { var tmp = db.Customers.FirstOrDefault(x => x.id == _id); if (tmp != null) { var listForDelete = db.Orders.Where(x => x.idCustomer == _id); foreach (var item in listForDelete) { db.Orders.Remove(item); } db.Customers.Remove(tmp); db.SaveChanges(); } else { Console.WriteLine("Error"); } } }