public virtual int Inserir(TEntity obj) { DbContext.Set <TEntity>().Add(obj); DbContext.SaveChanges(); return(obj.Id); }
public ActionResult Checkout(Purchases obj) { if (obj == null) { Response.Redirect("Index"); } List <Item> cart = (List <Item>)Session["cart"]; List <string> products = new List <string>(); for (int i = 0; i < cart.Count; i++) { products.Add(cart[i].Product.Id.ToString()); } string prodIdString = string.Join(",", products.ToArray()); try { using (MEContext context = new MEContext()) { obj.CustomerId = User.Identity.GetUserId(); obj.CustomerEmail = User.Identity.GetUserName(); obj.ProductId = prodIdString; obj.PurchaseDate = DateTime.Now; context.Purchases.Add(obj); context.SaveChanges(); Session["cart"] = null; return(Redirect("~/Home/Index/")); } } catch { return(View()); } }
private static void Teste1() { var opt = new DbContextOptionsBuilder <MEContext>(); opt.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MaterialEscolarDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"); using (var contexto = new MEContext(opt.Options)) { var prod1 = new Produto { Id = 1, Nome = "novo nome" }; contexto.Entry(prod1).State = EntityState.Modified; contexto.SaveChanges(); } }
public ActionResult Create(Product obj) { try { using (MEContext context = new MEContext()) { context.Products.Add(obj); context.SaveChanges(); return(Redirect("~/Product/Index")); } } catch { return(View()); } }
public ActionResult Edit(int id, FormCollection collection) { try { using (MEContext context = new MEContext()) { var c = context.Products.Find(id); TryUpdateModel(c); context.SaveChanges(); return(Redirect("~/Product/Index")); } } catch { return(View()); } }
public ActionResult Buy(int id) { using (MEContext context = new MEContext()) { if (Session["cart"] == null) { List <Item> cart = new List <Item>(); cart.Add(new Item { Product = context.Products.Find(id), Quanity = 1 }); Session["cart"] = cart; } else { List <Item> cart = (List <Item>)Session["cart"]; int index = isExist(id); if (index != -1) { cart[index].Quanity++; } else { cart.Add(new Item { Product = context.Products.Find(id), Quanity = 1 }); } Session["cart"] = cart; } var toChange = context.Products.Find(id); toChange.Qty -= 1; context.Entry(toChange).State = EntityState.Modified; context.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult Remove(int id) { List <Item> cart = (List <Item>)Session["cart"]; int index = isExist(id); int removedQty = cart[index].Quanity; cart.RemoveAt(index); if (cart.Count == 0) { cart = null; } Session["cart"] = cart; using (MEContext context = new MEContext()) { var toChange = context.Products.Find(id); toChange.Qty += removedQty; context.Entry(toChange).State = EntityState.Modified; context.SaveChanges(); } return(RedirectToAction("Index")); }