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 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")); }
public virtual void Atualizar(TEntity obj) { DbContext.Entry(obj).State = EntityState.Modified; DbContext.Entry(obj).Property(c => c.DataCriacao).IsModified = false; DbContext.SaveChanges(); }