private async static Task UserSaveObjectLastWins(string user, int delay, int starts) { using (var data = new Formula1Entities()) { int changes = 0; try { Racer r1 = data.Racers.Where(r => r.LastName == "Alonso").First(); r1.Starts = starts; await Task.Delay(delay); changes = data.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { Console.WriteLine("{0} error {1}", user, ex.Message); } Console.WriteLine("{0} changed {1} record(s)", user, changes); } }
private async static Task UserDealWithUpdates(string user, int delay, int starts) { using (var data = new Formula1Entities()) { int changes = 0; bool saveFailed = false; do { try { saveFailed = false; Racer r1 = data.Racers.Where(r => r.LastName == "Alonso").First(); r1.Starts = starts; await Task.Delay(delay); changes = data.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { saveFailed = true; Console.WriteLine("{0} error {1}", user, ex.Message); foreach (var entry in ex.Entries) { DbPropertyValues currentValues = entry.CurrentValues; DbPropertyValues databaseValues = entry.GetDatabaseValues(); DbPropertyValues resolvedValues = databaseValues.Clone(); AskUser(currentValues, databaseValues, resolvedValues); entry.OriginalValues.SetValues(databaseValues); entry.CurrentValues.SetValues(resolvedValues); } } }while (saveFailed); Console.WriteLine("{0} changed {1} record(s)", user, changes); } }