Exemple #1
0
        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);
            }
        }
Exemple #2
0
        private static void ChangeInformation()
        {
            using (var data = new Formula1Entities())
            {
                var esteban = data.Racers.Create();
                esteban.FirstName   = "Esteban";
                esteban.LastName    = "Gutierrez";
                esteban.Nationality = "Mexico";
                esteban.Starts      = 0;
                data.Racers.Add(esteban);

                Racer fernando = data.Racers.Where(r => r.LastName == "Alonso").First();
                fernando.Wins++;
                fernando.Starts++;

                foreach (DbEntityEntry <Racer> entry in data.ChangeTracker.Entries <Racer>())
                {
                    Console.WriteLine("{0}, state: {1}", entry.Entity, entry.State);
                    if (entry.State == EntityState.Modified)
                    {
                        Console.WriteLine("Original values");
                        DbPropertyValues values = entry.OriginalValues;
                        foreach (string propName in values.PropertyNames)
                        {
                            Console.WriteLine("{0} {1}", propName, values[propName]);
                        }
                        Console.WriteLine();
                        Console.WriteLine("Current values");

                        values = entry.CurrentValues;
                        foreach (string propName in values.PropertyNames)
                        {
                            Console.WriteLine("{0} {1}", propName, values[propName]);
                        }
                    }
                }
            }
        }