public void SaveProduct(Product entity) { if (entity.Id == default) { context.Entry(entity).State = EntityState.Added;// объект будет добавлен как новый } else { context.Entry(entity).State = EntityState.Modified;// старый объект будет изменён } context.SaveChanges(); }
public async Task <IActionResult> PutPayment([FromRoute] int id, [FromBody] Payment payment) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != payment.PaymentId) { return(BadRequest()); } _context.Entry(payment).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PaymentExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public IHttpActionResult PutPizza(int id, Pizza pizza) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != pizza.Id) { return(BadRequest()); } db.Entry(pizza).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!PizzaExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Edit([Bind(Include = "Id,Name")] Ingredient ingredient) { if (ModelState.IsValid) { db.Entry(ingredient).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(ingredient)); }