public IActionResult Edit([Bind("BeekeeperID,FirstName,LastName,Address,City,State,Zipcode,Phone,Email")] Beekeeper beekeeper) { if (ModelState.IsValid) { _context.Entry(beekeeper).State = EntityState.Modified; _context.SaveChanges(); return(RedirectToAction("Beekeepers")); } return(View(beekeeper)); }
public IActionResult Edit([Bind("BeekeeperIDFK,HiveName,InstallDate,Notes,HoneyProduction")] Beehive beehive) { if (ModelState.IsValid) { _context.Entry(beehive).State = EntityState.Modified; _context.SaveChanges(); try { // Attempt to save changes to the database _context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { foreach (var entry in ex.Entries) { if (entry.Entity is Beehive) { // Using a NoTracking query means we get the entity but it is not tracked by the context // and will not be merged with existing entities in the context. var databaseEntity = _context.Beehives.AsNoTracking().Single(p => p.BeehiveID == ((Beehive)entry.Entity).BeehiveID); var databaseEntry = _context.Entry(databaseEntity); foreach (var property in entry.Metadata.GetProperties()) { var proposedValue = entry.Property(property.Name).CurrentValue; var originalValue = entry.Property(property.Name).OriginalValue; var databaseValue = databaseEntry.Property(property.Name).CurrentValue; // TODO: Logic to decide which value should be written to database // entry.Property(property.Name).CurrentValue = <value to be saved>; // Update original values to entry.Property(property.Name).OriginalValue = databaseEntry.Property(property.Name).CurrentValue; } } else { throw new NotSupportedException("Don't know how to handle concurrency conflicts for " + entry.Metadata.Name); } } // Retry the save operation _context.SaveChanges(); } return(RedirectToAction("Beehives", routeValues: new { id = beehive.BeekeeperIDFK })); } return(View(beehive)); }