public async Task <IActionResult> PutProduct(int id, Product product) { if (id != product.ProductId) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public void UpdateProduct(Product info) { using (var context = new NWContext()) { context.Products.Attach(info); context.Entry(info).State = EntityState.Modified; context.SaveChanges(); } }
public void Delete(Products products) { using (NWContext context = new NWContext()) { var entity = context.Entry(products); entity.State = EntityState.Deleted; context.SaveChanges(); } }
//Update //change the entire entity record //it does not matter LOGICALLY that you change a value to itself //by changing the entire entity, you change all fields that need to be altered //the value return is the number of rows affected public int Products_Update(Product item) { using (var context = new NWContext()) { //Staging context.Entry(item).State = System.Data.Entity.EntityState.Modified; //Commit and Feedback (rows affected) return(context.SaveChanges()); } }
public void Add(Products products) { using (NWContext context = new NWContext()) { //context.Products.Add(products); var entity = context.Entry(products); entity.State = EntityState.Added; context.SaveChanges(); } }
public void UpdateProduct(Product info) { // See question and commentary on http://stackoverflow.com/questions/15336248/entity-framwork-5-updating-a-record using (var context = new NWContext()) { context.Products.Attach(info); context.Entry(info).State = EntityState.Modified; context.SaveChanges(); } }
public void UpdateShipper(Shipper info) { //Note: see question and commentary on //http://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-reocrd using (var context = new NWContext()) { context.Shippers.Attach(info); context.Entry(info).State = EntityState.Modified; context.SaveChanges(); } }
public void UpdateShipper(Shipper info) { //throw new NotImplementedException(); //see qns and comentary on //stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record using (var context = new NWContext()) { context.Shippers.Attach(info); context.Entry(info).State = EntityState.Modified; context.SaveChanges(); } }
public void UpdateShipper(Shipper info) { // note see commentary on // http://stackoverflow.com/questions/15336248/entity=framework-5-updating-a-record // TO do give this away i am on vacation using (var context = new NWContext()) { context.Shippers.Attach(info); context.Entry(info).State = EntityState.Modified; context.SaveChanges(); } }
public void UpdateShipper(Product info) { //throw new NotImplementedException(); //todo: give this to the new guy - i'm on holidays :) :) // http://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record using (var context = new NWContext()) { context.Products.Attach(info); context.Entry(info).State = EntityState.Modified; context.SaveChanges(); } }
public void Update(Region region, List <Territory> territories) { // TODO: Add Unit Test if (region == null) { throw new ArgumentNullException("region", "region is null."); } if (territories == null) { throw new ArgumentNullException("territories", "territories is null."); } using (var dbContext = new NWContext()) { foreach (var item in territories) { var found = dbContext.Territories.Find(item.TerritoryID); if (found != null) { /* NOTE: * Pre-process the Territory IDs to see if they should be "synced" with the name/description. * This will be the case if, in the original, the ID was the same as the description */ string foundTerritoryID = found.TerritoryID; string foundTerritoryDescription = found.TerritoryDescription.Trim(); // HACK: Turns out, the column is nchar(50), not an nvarchar.... string itemTerritoryID = item.TerritoryID; string itemTerritoryDescription = item.TerritoryDescription.Trim(); if (foundTerritoryID.Equals(foundTerritoryDescription) && !itemTerritoryID.Equals(itemTerritoryDescription)) { item.TerritoryID = itemTerritoryDescription; dbContext.Territories.Remove(found); // Because the PK has changed... dbContext.Territories.Add(item); // Because the PK has changed... } } } dbContext.Entry(region).State = EntityState.Modified; dbContext.SaveChanges(); } }
//Delete //delete (physical) or change (logical delete really and update) the entire entity record //the value returned is the number of rows affected public int Products_Delete(int productid) { using (var context = new NWContext()) { //Physical Delete //the physical removal of a record from the database ////locate the instance of the entity to be removed //var existing = context.Products.Find(productid); ////optional check to see if it is there ////if not throw an exception. ////this process can also be handled on the web form during feedback //if (existing == null) //{ // throw new Exception("Record has been remove from database"); //} ////Stage //context.Products.Remove(existing); ////Commmit and feedback //return context.SaveChanges(); //Logical delete //you normally set a peroperty to a specific value to // indicate the record should be considered "gone" //this is actually an update of the record //locate the instance of the entity to be changed var existing = context.Products.Find(productid); //set the property to the specific value existing.Discontinued = true; //Stage an update context.Entry(existing).State = System.Data.Entity.EntityState.Modified; //Commit and feedback return(context.SaveChanges()); } }
public IActionResult Put(int id, [FromBody] Employee obj) { db.Entry <Employee>(obj).State = EntityState.Modified; db.SaveChanges(); return(new ObjectResult("Modified!")); }