private void UpdateCustomerWithApplyCurrentValues(Customer cust) { using (var ctx = new OrderITEntities()) { var dbCust = ctx.Companies.OfType <Customer>().First(c => c.CompanyId == cust.CompanyId); ctx.Companies.ApplyCurrentValues(cust); var entry = ctx.ObjectStateManager.GetObjectStateEntry(dbCust); var origValues = entry.GetUpdatableOriginalValues(); origValues.SetValue(origValues.GetOrdinal("Version"), cust.Version); try { ctx.SaveChanges(); MessageBox.Show("Customer updated"); } catch (OptimisticConcurrencyException ex) { var errorEntry = ex.StateEntries.First(); ctx.Refresh(RefreshMode.ClientWins, errorEntry.Entity); var form = new EntriesComparer(errorEntry); form.ShowDialog(); if (form.ApplyChanges) { ctx.SaveChanges(); MessageBox.Show("Customer updated"); } } } }
private void btnUpdateCustomerConnected_Click(object sender, EventArgs e) { using (var ctx = new OrderITEntities()) { var id = Convert.ToInt32(CustomerId.Text); var cust = ctx.Companies.OfType <Customer>().First(c => c.CompanyId == id); cust.Name = CustomerName.Text; cust.WSEnabled = WSEnabled.Checked; cust.WSUserName = WSUsername.Text; cust.WSPassword = WSPassword.Text; cust.BillingAddress.Address = BillingAddress.Text; cust.BillingAddress.City = BillingCity.Text; cust.BillingAddress.Country = BillingCountry.Text; cust.BillingAddress.ZipCode = BillingZipCode.Text; cust.ShippingAddress.Address = ShippingAddress.Text; cust.ShippingAddress.City = ShippingCity.Text; cust.ShippingAddress.Country = ShippingCountry.Text; cust.ShippingAddress.ZipCode = ShippingZipCode.Text; try { ctx.SaveChanges(); MessageBox.Show("Customer updated"); } catch (OptimisticConcurrencyException ex) { var errorEntry = ex.StateEntries.First(); ctx.Refresh(RefreshMode.ClientWins, errorEntry.Entity); var form = new EntriesComparer(errorEntry); form.ShowDialog(); if (form.ApplyChanges) { ctx.SaveChanges(); MessageBox.Show("Customer updated"); } } } }
private void UpdateCustomerWithChangeObjectState(Customer cust) { using (var ctx = new OrderITEntities()) { ctx.Companies.Attach(cust); ctx.ObjectStateManager.ChangeObjectState(cust, EntityState.Modified); try { ctx.SaveChanges(); MessageBox.Show("Customer updated"); } catch (OptimisticConcurrencyException ex) { var errorEntry = ex.StateEntries.First(); ctx.Refresh(RefreshMode.ClientWins, errorEntry.Entity); var form = new EntriesComparer(errorEntry); form.ShowDialog(); if (form.ApplyChanges) { ctx.SaveChanges(); MessageBox.Show("Customer updated"); } } } }