public ActionResult Checkout(client client) { List <Cart> cart = (List <Cart>)Session["cart"]; decimal? grandTotal = cart.Sum(x => x.Total); db.clients.Add(client); db.SaveChanges(); order o = new order(); o.OrderDate = DateTime.Now; o.ClientID = db.clients.Select(x => x.ClientID).ToList().LastOrDefault(); //o.ClientID = (WALA PA LAMAN SA DB MO TO :D) //o.PaymentID = (WALA PA LAMAN SA DB MO TO :D) db.orders.Add(o); foreach (var item in cart) { OrderDetail od = new OrderDetail(); od.OrderID = o.OrderID; od.ProductID = item.ProductID; od.Quantity = item.Quantity; db.OrderDetails.Add(od); product p = db.products.Where(x => x.ProductID == item.ProductID).FirstOrDefault(); p.Quantity -= item.Quantity; } db.SaveChanges(); return(View("Done", cart)); }
/// <summary> /// Inserts data with EntityFramework /// </summary> /// <param name="seed">Randomnumber generators seed</param> public static void InsertData(int seedID, int rows) { try { var sw = new Stopwatch(); Console.WriteLine("--Inserting data!"); sw.Start(); var i = 0; var result = false; var seed = 0; using (var db = new ThesisEntities()) { seed = (from sd in db.Seed where sd.SeedID == seedID select sd.SeedValue).FirstOrDefault(); Console.WriteLine("--Seed is : " + seed); var gen = new Random(seed); while (i < rows) { var row = new RandomObject() { RandomObjectID = i, RandomString = RandomStringGenerator.RandomString(gen, 15), RandomDateTimeOffset = RandomDateTimeOffsetGenerator.RandomDateTimeOffset(gen), RandomInt = gen.Next(), SeedId = seedID }; db.RandomObject.Add(row); i++; } db.SaveChanges(); result = true; sw.Stop(); } Console.WriteLine("--Result was: " + result); Console.WriteLine("--Time Elapsed: " + sw.Elapsed + "\n"); } catch (Exception ex) { Console.WriteLine("Exception occured: " + ex); Console.WriteLine("Press any key to continue."); Console.ReadKey(); } }
public ActionResult Add(NewProduct model) { if (ModelState.IsValid) { try { product product = new product(); product.ProductName = model.ProductName; product.CategoryID = model.CategoryID; product.ProductDescription = model.ProductDescription; product.ProductPrice = model.ProductPrice; HttpPostedFileBase file = Request.Files["image"]; product.ProductPicture = file.ContentLength != 0 ? ConvertToByte(file) : null; db.products.Add(product); db.SaveChanges(); return(RedirectToAction("List")); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } else { model = new NewProduct() { GetProductCategory = GetProductCategory() }; return(View(model)); } }