public IActionResult Update(Product product) { if (!ModelState.IsValid) { return(View(product)); } _productsDb.Entry(product).State = EntityState.Modified; _productsDb.SaveChanges(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> PutProduct([FromRoute] int id, [FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.ID) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
/// <summary> /// Update the product /// </summary> /// <param name="product">Product to update</param> /// <returns>How many changes occurred during the update</returns> public int UpdateProduct(Product product) { var entity = DBContext.Products.Find(product.Id); DBContext.Entry(entity).CurrentValues.SetValues(product); return(DBContext.SaveChanges(true)); }
public async Task <IActionResult> PutCustomer(int id, Customer customer) //content-type { if (id != customer.Id) { return(BadRequest()); } _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <Product> UpdateProduct(Product product) { try { if (product != null) { bool isProductExistInDb = await IsProductExistInDb(product.Id); if (isProductExistInDb && product.Id != Guid.Empty) { _context.Entry(product).State = EntityState.Modified; await _context.SaveChangesAsync(); return(product); } else { return(null); } } } catch (Exception) { return(null); } return(null); }
public void Update(Product item) { if (item == null) { return; } _context.Entry(item).State = EntityState.Modified; }
public Catalogue UpdateCatalogue(Catalogue catalogue) { //using (ProductsDbContext context = new ProductsDbContext()) { Catalogue updatedCatalogue = _context.Catalogues.Find(catalogue.Id); _context.Entry(updatedCatalogue).CurrentValues.SetValues(catalogue); _context.SaveChanges(); return(updatedCatalogue); } }
public ActionResult Edit([Bind(Include = "Id,Title")] Product product) { if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); }
public bool Create(T entity) { bool created = false; try { //_context.Set<T>().Add(entity); _context.Entry(entity).State = EntityState.Added; _context.SaveChanges(); created = true; } catch (Exception) { created = false; throw; } return(created); }
public IHttpActionResult PutProduct(Product product) { db.Entry(product).State = System.Data.Entity.EntityState.Modified; string sqlUpdate = "EXEC UpdateProcedure " + product.Id + "," + product.Name + "," + product.Category + "," + product.Price; db.Database.ExecuteSqlCommand(sqlUpdate); db.SaveChanges(); return(this.Json(new { msg = "success" })); }
/// <summary> /// Update product /// </summary> /// <param name="product"></param> /// <returns>True if successful, else false</returns> public async Task <bool> UpdateProductAsync(Product product) { try { _context.Entry(product).State = EntityState.Modified; await _context.SaveChangesAsync(); return(true); } catch (Exception) { return(false); } }
private static void SetPersistenceMethod <T>(BuilderSettings builderSettings) where T : class { builderSettings.SetCreatePersistenceMethod <T>((item) => { using (var db = new ProductsDbContext()) { db.Set <T>().Attach(item); db.Set <T>().Add(item); db.SaveChanges(); } }); builderSettings.SetCreatePersistenceMethod <IList <T> >((items) => { using (var db = new ProductsDbContext()) { foreach (var item in items) { db.Set <T>().Attach(item); db.Set <T>().Add(item); } db.SaveChanges(); } }); builderSettings.SetUpdatePersistenceMethod <T>(item => { using (var db = new ProductsDbContext()) { db.Set <T>().Attach(item); db.Entry(item).State = EntityState.Modified; db.SaveChanges(); } }); builderSettings.SetUpdatePersistenceMethod <IList <T> >((items) => { using (var db = new ProductsDbContext()) { foreach (var item in items) { db.Set <T>().Attach(item); db.Entry(item).State = EntityState.Modified; db.Set <T>().Add(item); } db.SaveChanges(); } }); }
public bool Update(Product item) { var currentProduct = context.Products.Find(item.Id); if (currentProduct is null) { return(false); } currentProduct.Name = item.Name; currentProduct.Category = item.Category; currentProduct.Price = item.Price; context.Entry(currentProduct).State = EntityState.Modified; context.SaveChanges(); return(true); }
public bool Create <T>(T record) where T : class { bool created = false; try { _context.Entry(record).State = EntityState.Added; _context.SaveChanges(); created = true; } catch (Exception) { created = false; throw; } return(created); }
public async Task <IActionResult> Put(int id, [FromBody] Product product) { try { if (id != product.Id) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; await _context.SaveChangesAsync(); return(NoContent()); } catch (Exception exception) { return(BadRequest(exception)); } }
public void EditCompanyStock(CompanyStock companyStock) { productsDb.Entry(companyStock).State = Microsoft.EntityFrameworkCore.EntityState.Modified; productsDb.SaveChanges(); }
public void EditWorkStock(WorkStock workStock) { context.Entry(workStock).State = Microsoft.EntityFrameworkCore.EntityState.Modified; context.SaveChanges(); }
public ActionResult Edit(Products entity) { var categories = db.Categories.ToList(); var selectList2 = new SelectList(categories, "Id", "Name"); ViewBag.Categories = selectList2; var companies = db.Companies.ToList(); var selectList = new SelectList(companies, "Id", "Name"); ViewBag.Companies = selectList; var countries = db.Countries.ToList(); var selectList1 = new SelectList(countries, "Id", "Name"); ViewBag.Countries = selectList1; if (ModelState.IsValid) { } if (entity.BrandName == null) { ViewBag.Message = "لطفا نام برند را وارد نمایید."; return(View(entity)); } if (entity.ModelNo == null) { ViewBag.Message = "لطفا شماره محصول را وارد نمایید."; return(View(entity)); } if (entity.CategoryId == null) { ViewBag.Message = "لطفا نوع دسته را انتخاب نمایید."; return(View(entity)); } if (entity.CompanyId == null) { ViewBag.Message = "لطفا شرکت سازنده را انتخاب نمایید"; return(View(entity)); } if (entity.CountryId == null) { ViewBag.Message = "لطفا کشورمورد نظررا انتخاب نمایید"; return(View(entity)); } db.Entry(entity).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); }