public Order Update(Order orderUpdate) { _ctx.Attach(orderUpdate).State = EntityState.Modified; _ctx.Entry(orderUpdate).Reference(o => o.Customer).IsModified = true; _ctx.SaveChanges(); return(orderUpdate); /* * // var changeTracker = _ctx.ChangeTracker.Entries<Customer>(); //very smart for debugging * if (orderUpdate.Customer != null && * _ctx.ChangeTracker.Entries<Customer>() //for Disconnected Entity * .FirstOrDefault(ce => ce.Entity.Id == orderUpdate.Customer.Id) == null) * { * //Attaches order to the given customer with the input ID (PostMan) * //instead of creating a new customer with that ID * _ctx.Attach(orderUpdate.Customer); * } * else * { * //To remove customer from the order in an update request * _ctx.Entry(orderUpdate).Reference(o => o.Customer).IsModified = true; * } * var Updated = _ctx.Orders.Update(orderUpdate).Entity; * _ctx.SaveChanges(); * return Updated; */ }
public Fine Update(Fine fineUpdate) { _context.Attach(fineUpdate).State = Microsoft.EntityFrameworkCore.EntityState.Modified; _context.Entry(fineUpdate).Reference(o => o.Member).IsModified = true; _context.SaveChangesAsync(); return(fineUpdate); }
public Customer Update(Customer customerUpdate) { _ctx.Attach(customerUpdate).State = EntityState.Modified; _ctx.Entry(customerUpdate).Collection(c => c.Orders).IsModified = true; _ctx.Entry(customerUpdate).Reference(c => c.Type).IsModified = true; var orders = _ctx.Orders.Where(o => o.Customer.Id == customerUpdate.Id && !customerUpdate.Orders.Exists(co => co.Id == o.Id)); foreach (var order in orders) { order.Customer = null; _ctx.Entry(order).Reference(o => o.Customer) .IsModified = true; } _ctx.SaveChanges(); return(customerUpdate); }
public Order Create(Order order) { if (order.Customer != null) { _context.Entry(order.Customer).State = EntityState.Unchanged; } _context.Orders.Add(order); return(order); }
public ActionResult Edit([Bind(Include = "Id,Name,City,State,TotalSales")] Customer customer) { if (ModelState.IsValid) { db.Entry(customer).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(customer)); }
public ActionResult Edit([Bind(Include = "Id,Description,Total,CustomerId")] Order order) { if (ModelState.IsValid) { db.Entry(order).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CustomerId = new SelectList(db.Customers, "Id", "Name", order.CustomerId); return(View(order)); }
public ActionResult Edit([Bind(Include = "Id,OrderId,Product,Quantity,Price")] OrderLine orderLine) { if (ModelState.IsValid) { db.Entry(orderLine).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.OrderId = new SelectList(db.Orders, "Id", "Description", orderLine.OrderId); return(View(orderLine)); }
public Order Update(Order orderUpdate) { //Clone orderlines to new location in memory, so they are not overridden on Attach var newOrderLines = new List <OrderLine>(orderUpdate.OrderLines); //Attach order so basic properties are updated _ctx.Entry(orderUpdate).State = EntityState.Modified; //Remove all orderlines with updated order information _ctx.OrderLines.RemoveRange( _ctx.OrderLines.Where(ol => ol.OrderId == orderUpdate.Id) ); //Add all orderlines with updated order information foreach (var ol in newOrderLines) { _ctx.Entry(ol).State = EntityState.Added; } //Update customer relation _ctx.Entry(orderUpdate).Reference(o => o.Customer).IsModified = true; // Save it _ctx.SaveChanges(); //Return it return(orderUpdate); }
public Order Update(Order orderUpdate) { /*if(orderUpdate.Customer != null && * _ctx.ChangeTracker.Entries<Customer>() * .FirstOrDefault(ce => ce.Entity.ID == orderUpdate.Customer.ID) == null) * { * _ctx.Attach(orderUpdate.Customer); * } * else * { * _ctx.Entry(orderUpdate).Reference(o => o.Customer).IsModified = true; * } * var updated = _ctx.Update(orderUpdate).Entity; * _ctx.SaveChanges(); * return updated;*/ _ctx.Attach(orderUpdate).State = EntityState.Modified; _ctx.Entry(orderUpdate).Reference(o => o.Customer).IsModified = true; _ctx.SaveChanges(); return(orderUpdate); }
public Product Update(Product productUpdate) { //Clone orderlines to new location in memory, so they are not overridden on Attach var newOrderLines = new List <OrderLine>(productUpdate.OrderLines); //Attach product so basic properties are updated _ctx.Attach(productUpdate).State = EntityState.Modified; //Remove all orderlines with updated order information _ctx.OrderLines.RemoveRange( _ctx.OrderLines.Where(ol => ol.ProductId == productUpdate.Id) ); //Add all orderlines with updated order information foreach (var ol in newOrderLines) { _ctx.Entry(ol).State = EntityState.Added; } // Save it _ctx.SaveChanges(); //Return it return(productUpdate); }
public void Edit(User entity) { db.Entry(entity).State = EntityState.Modified; db.SaveChanges(); }