/* (CLIENT-WINS) OVERRIDING: Resolving optimistic concurrency exceptions as Client-Wins */ // The override method can be used to override persisted data with the supplied entity data. public void UpdateCustomerOverride(Customer customer) { bool saveFailed; this.context.Entry(customer).State = EntityState.Modified; do { saveFailed = false; try { this.context.SaveChanges(); } catch (DbUpdateConcurrencyException e) { saveFailed = true; // Each entry has three distinct sets of values, // CurrentValues (The values currently contained in the entity) // OriginalValues (The values read from the database upon commitment) // DatabaseValues (The values currently persisted in the database) // Set the original values of the entity to the values stored in the database // avoiding another exception with regards to concurrency. var entry = e.Entries.Single(); entry.OriginalValues.SetValues(entry.GetDatabaseValues()); } } while (saveFailed); }
public void InsertCustomer(Customer customer) { ////AfterHandInChange - attach battery in order not to insert a duplicate battery if (customer.Battery != null) { this.context.Batteries.Attach(customer.Battery); } this.context.Customers.Add(customer); }
public void UpdateCustomer(Customer customer) { try { this.customerRepository.UpdateCustomer(customer); this.customerRepository.Save(); } catch (DbUpdateConcurrencyException e) { throw new FaultException(e.Message); } catch (Exception e) { throw new FaultException(e.Message); } }
public void InsertCustomer(string name, string streetAddress, string phoneNr, string zipCodeID, int batteryId) { ////AfterHandInChange - update battery BatteryCtr batteryCtr = new BatteryCtr(); var battery = batteryCtr.FindBattery(batteryId); var customer = new Customer { Name = name, StreetAddress = streetAddress, PhoneNr = phoneNr, ZipCodeNumber = zipCodeID, BatteryId = batteryId, Battery = battery }; ////AfterHandInChange - update battery battery.BatteryStation = null; battery.BatteryStationID = null; batteryCtr.UpdateBattery(battery); this.customerRepository.InsertCustomer(customer); this.customerRepository.Save(); }
public void UpdateCustomer(Customer customer) { controller.UpdateCustomer(customer); }
public void UpdateCustomer(Customer customer) { this.context.Entry(customer).State = EntityState.Modified; }
/* (DATABASE-WINS) RELOADING: Resolving optimistic concurrency exceptions with Reload */ // The Reload method can be used to override the values contained in the entity, // and can be designed to return a new entity containing persisted data to the caller. public Customer UpdateCustomerReload(Customer customer) { bool saveFailed; do { saveFailed = false; this.context.Entry(customer).State = EntityState.Modified; try { this.context.SaveChanges(); } catch (DbUpdateConcurrencyException e) { saveFailed = true; // Update the supplied customer with persisted data e.Entries.Single().Reload(); } } while (saveFailed); return customer; }
/* (CLIENT-CHOOSES) OVERRIDING: Resolving optimistic concurrency exception as Client-Choses */ // The override method can be used to override persisted data, on behest of the client. public void UpdateCustomerOverrideClientChooses(Customer customer) { bool saveFailed; this.context.Entry(customer).State = EntityState.Modified; do { saveFailed = false; try { this.context.SaveChanges(); } catch (DbUpdateConcurrencyException e) { saveFailed = true; // Get the current entity var entry = e.Entries.Single(); // Get the current values for the entity var currentValues = entry.CurrentValues; // Get the persisted values from the database var databaseValues = entry.GetDatabaseValues(); // Choose an initial set of resolved values. In this case // make the default be the values currently in the database. var resolvedValues = databaseValues.Clone(); // Have the client choose what the resolved values should be HaveUserResolveConcurrency(currentValues, databaseValues, resolvedValues); // Update the original values with the database values and // the current values with whatever the client choose. entry.OriginalValues.SetValues(databaseValues); entry.CurrentValues.SetValues(resolvedValues); } } while (saveFailed); }