public void FixStateCanInterpretLocalState() { var cart = NewCart.CreateCartFromProductSelection(_theUri, null, 1, 1, 9.99m); cart.CartItems.First().UpdateQuantity(2); _context.Carts.Attach(cart); _context.FixState(); Assert.AreEqual(EntityState.Unchanged, _context.Entry(cart).State); Assert.AreEqual(EntityState.Modified, _context.Entry(cart.CartItems.First()).State); }
public IHttpActionResult PutCategory(int id, Category category) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != category.CategoryID) { return(BadRequest()); } db.Entry(category).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutCart(int id, Cart cart) { if (id != cart.CartId) { return(BadRequest()); } _context.Entry(cart).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CartExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
/// <summary> /// /// </summary> /// <param name="shoppingCart"></param> /// <returns></returns> public async Task <ShoppingCart> UpdateItem(ShoppingCart shoppingCart) { try { var results = await _context.ShoppingCarts.FindAsync(shoppingCart.Id); if (results == null) { return(new ShoppingCart { Message = $"User with Id: {shoppingCart.Id} does not exist in shopping cart" }); } //To solve the problem of tracking entity. Make sure entity is in stable state _context.Entry(results).State = EntityState.Detached; _dataBaseChanges.Update(shoppingCart); await _dataBaseChanges.CommitAsync(); } catch (Exception ex) { _logger.LogError($"An error occured during getting item from DB => {ex.InnerException}"); } return(new ShoppingCart { Message = $"Successfully updated user with Id:{shoppingCart.Id}" }); }
public IHttpActionResult PutProduct(int id, Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.ProductID) { return(BadRequest()); } db.Entry(product).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public CommodityItem AddToShoppingCart(CommodityItem item) { CommodityItem existingItem = GetByShoppingCartIdAndBookId(item.ShoppingCartId, item.BookId); if (existingItem == null) { _db.Entry(item).State = EntityState.Added; existingItem = item; } else { existingItem.Quantity += item.Quantity; } _db.SaveChanges(); return(existingItem); }
public CartItem Add2Cart(CartItem cartItem) { var existingCartItem = GetByCartIdAndBookId(cartItem.CartId, cartItem.BookId); if (existingCartItem == null) { _db.Entry(cartItem).State = EntityState.Added; existingCartItem = cartItem; } else { existingCartItem.Quantity += cartItem.Quantity; } _db.SaveChanges(); return(existingCartItem); }
public async Task <IActionResult> PutCustomers(Guid id, Customers customers) { if (id != customers.Id) { return(BadRequest()); } _context.Entry(customers).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomersExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> public void Update <T>(T obj) where T : class { var set = _context.Set <T>(); set.Attach(obj); _context.Entry(obj).State = EntityState.Modified; }
public async Task <bool> Update(Product entity) { try { _context.Entry(entity).State = EntityState.Modified; await _context.SaveChangesAsync(); return(true); } catch (Exception) { return(false); } }
public void Save(Cart Cart) { try { using (_context = new ShoppingCartContext()) { _context.Cart.Add(Cart); if (Cart.ID > 0) { _context.Entry(Cart).State = EntityState.Modified; } _context.SaveChanges(); } } catch (Exception ex) { } }
// PUT api/Todo/5 public HttpResponseMessage PutTodo(int id, Todo todo) { if (ModelState.IsValid && id == todo.Id) { db.Entry(todo).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } }
public void EditProduct(Product product) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); }
public void Add(T entity) { EntityEntry dbEntityEntry = context.Entry <T>(entity); context.Set <T>().Add(entity); }
public async Task <ActionResult> EditProduct(EditProductViewModel model) { var id = (int)TempData["ProductEditId"]; if (!ModelState.IsValid) { return(View(model)); } using (var context = new ShoppingCartContext()) { //new product if (id == -1) { var newProd = new Product() { Code = SeqHelper.Next("Item").ToString(), //Code = (string.IsNullOrEmpty(model.ProductView.Code)) ? SeqHelper.Next("Item").ToString() : (model.ProductView.Code), Description = model.ProductView.Description, FeatureProduct = true, //Weight = 0.5m, //QuantityOnHand = 22, BuyInPrice = model.ProductView.BuyInPrice, DetailDescription = model.ProductView.DetailDescription, Categories = new List <Category>() { context.Categories.SingleOrDefault(c => c.Code == "1001") }, Active = model.ProductView.Active, Notes = model.ProductView.Notes, ReviewVideoUrl = (model.ProductView.ReviewVideoUrl.IsNullOrWhiteSpace()) ? "" : model.ProductView.ReviewVideoUrl }; if (model.ProductView.ProductImage != null) { if (model.ProductView.ProductImage.ContentLength > (4 * 1024 * 1024)) { ModelState.AddModelError("CustomError", "Image can not be lager than 4MB."); return(View()); } if ( !(model.ProductView.ProductImage.ContentType == "image/jpeg" || model.ProductView.ProductImage.ContentType == "image/gif")) { ModelState.AddModelError("CustomError", "Image must be in jpeg or gif format."); return(View()); } byte[] data = new byte[model.ProductView.ProductImage.ContentLength]; model.ProductView.ProductImage.InputStream.Read(data, 0, model.ProductView.ProductImage.ContentLength); newProd.Image = data; } //alt image 0 if (model.ProductView.ProductImageAlt0 != null) { if (model.ProductView.ProductImageAlt0.ContentLength > (4 * 1024 * 1024)) { ModelState.AddModelError("CustomError", "Image can not be lager than 4MB."); return(View()); } if ( !(model.ProductView.ProductImageAlt0.ContentType == "image/jpeg" || model.ProductView.ProductImageAlt0.ContentType == "image/gif")) { ModelState.AddModelError("CustomError", "Image must be in jpeg or gif format."); } byte[] data = new byte[model.ProductView.ProductImageAlt0.ContentLength]; model.ProductView.ProductImageAlt0.InputStream.Read(data, 0, model.ProductView.ProductImageAlt0.ContentLength); newProd.ImageAlt0 = data; } //alt image 1 if (model.ProductView.ProductImageAlt1 != null) { if (model.ProductView.ProductImageAlt1.ContentLength > (4 * 1024 * 1024)) { ModelState.AddModelError("CustomError", "Image can not be lager than 4MB."); return(View()); } if ( !(model.ProductView.ProductImageAlt1.ContentType == "image/jpeg" || model.ProductView.ProductImageAlt1.ContentType == "image/gif")) { ModelState.AddModelError("CustomError", "Image must be in jpeg or gif format."); } byte[] data = new byte[model.ProductView.ProductImageAlt1.ContentLength]; model.ProductView.ProductImageAlt1.InputStream.Read(data, 0, model.ProductView.ProductImageAlt1.ContentLength); newProd.ImageAlt1 = data; } context.Products.Add(newProd); await context.SaveChangesAsync(); id = newProd.Id; //update pricing foreach (var p in model.Offers) { context.ProductOffers.Add(new ProductOffer() { ProductId = id, PriceTypeId = p.PriceTypeId, Price = p.Price }); } //weight decimal ounces = model.ProductView.WeightOunce; decimal lbs = model.ProductView.WeightPounds; decimal weight = lbs + (ounces / 16); newProd.Weight = weight; //quantity on hand newProd.QuantityOnHand = model.ProductView.QuantityOnHand; //categories foreach (var cat in model.Categories) { if (cat.IsChecked) { newProd.Categories.Add(context.Categories.Single(c => c.Code == cat.Code)); } } await context.SaveChangesAsync(); //return View("Index"); //return RedirectToAction("EditProduct", newProd.Id); } else { //existing product var product = context.Products.Single(p => p.Id == id); //string paymentTypeValue = Request.Form["paymentType"].ToString(); product.Description = model.ProductView.Description; //product.Code = model.ProductView.Code; product.DetailDescription = model.ProductView.DetailDescription; product.BuyInPrice = model.ProductView.BuyInPrice; product.Active = model.ProductView.Active; product.Notes = model.ProductView.Notes; product.ReviewVideoUrl = (model.ProductView.ReviewVideoUrl.IsNullOrWhiteSpace()) ? "" : model.ProductView.ReviewVideoUrl; if (model.ProductView.ProductImage != null) { if (model.ProductView.ProductImage.ContentLength > (4 * 1024 * 1024)) { ModelState.AddModelError("CustomError", "Image can not be lager than 4MB."); return(View()); } if ( !(model.ProductView.ProductImage.ContentType == "image/jpeg" || model.ProductView.ProductImage.ContentType == "image/gif")) { ModelState.AddModelError("CustomError", "Image must be in jpeg or gif format."); } byte[] data = new byte[model.ProductView.ProductImage.ContentLength]; model.ProductView.ProductImage.InputStream.Read(data, 0, model.ProductView.ProductImage.ContentLength); product.Image = data; } //alt image 0 if (model.ProductView.ProductImageAlt0 != null) { if (model.ProductView.ProductImageAlt0.ContentLength > (4 * 1024 * 1024)) { ModelState.AddModelError("CustomError", "Image can not be lager than 4MB."); return(View()); } if ( !(model.ProductView.ProductImageAlt0.ContentType == "image/jpeg" || model.ProductView.ProductImageAlt0.ContentType == "image/gif")) { ModelState.AddModelError("CustomError", "Image must be in jpeg or gif format."); } byte[] data = new byte[model.ProductView.ProductImageAlt0.ContentLength]; model.ProductView.ProductImageAlt0.InputStream.Read(data, 0, model.ProductView.ProductImageAlt0.ContentLength); product.ImageAlt0 = data; } //alt image 1 if (model.ProductView.ProductImageAlt1 != null) { if (model.ProductView.ProductImageAlt1.ContentLength > (4 * 1024 * 1024)) { ModelState.AddModelError("CustomError", "Image can not be lager than 4MB."); return(View()); } if ( !(model.ProductView.ProductImageAlt1.ContentType == "image/jpeg" || model.ProductView.ProductImageAlt1.ContentType == "image/gif")) { ModelState.AddModelError("CustomError", "Image must be in jpeg or gif format."); } byte[] data = new byte[model.ProductView.ProductImageAlt1.ContentLength]; model.ProductView.ProductImageAlt1.InputStream.Read(data, 0, model.ProductView.ProductImageAlt1.ContentLength); product.ImageAlt1 = data; } //update pricing foreach (var p in model.Offers) { var update = context.ProductOffers.SingleOrDefault( po => po.ProductId == product.Id && p.PriceTypeId == po.PriceTypeId); if (update != null) { update.Price = p.Price; } else { context.ProductOffers.Add(new ProductOffer() { ProductId = product.Id, PriceTypeId = p.PriceTypeId, Discountable = true, Price = p.Price, Code = product.Code + "-" + "O" }); } } //weight decimal ounces = model.ProductView.WeightOunce; decimal lbs = model.ProductView.WeightPounds; decimal weight = lbs + (ounces / 16); product.Weight = weight; //quantity on hand product.QuantityOnHand = model.ProductView.QuantityOnHand; //categories model.ProductView.Categories.AddRange( context.Products.Where(p => p.Id == id).SelectMany(prod => prod.Categories)); var setCat = model.ProductView.Categories; //load both collection before removing many-to-many can work ! context.Entry(product).Collection("Categories").Load(); foreach (var cat in model.Categories) { if (cat.IsChecked && !setCat.Select(c => c.Code).ToList().Contains(cat.Code)) { product.Categories.Add(context.Categories.Single(c => c.Code == cat.Code)); } if (!cat.IsChecked && setCat.Select(c => c.Code).ToList().Contains(cat.Code)) { product.Categories.Remove(context.Categories.Single(c => c.Code == cat.Code)); } } await context.SaveChangesAsync(); } return(RedirectToAction("EditProduct", id)); } }
public virtual void Edit(T entity) { _context.Entry(entity).State = EntityState.Modified; }
public void editStoreManagerDBRecord(StoreManager storeManager) { db.Entry(storeManager).State = EntityState.Modified; db.SaveChanges(); }