示例#1
0
 private static void ChangeInformation()
 {
     using (var data = new Formula1Entities())
     {
         var jaime = new Racer
         {
             Firstname = "Jaime",
             Lastname  = "Alguersuari",
             Country   = "Spain",
             Starts    = 0
         };
         data.Racers.AddObject(jaime);
         Racer fernando = data.Racers.Where("it.Lastname='Alonso'").First();
         fernando.Starts++;
         DisplayState(EntityState.Added.ToString(),
                      data.ObjectStateManager.GetObjectStateEntries(EntityState.Added));
         DisplayState(EntityState.Modified.ToString(),
                      data.ObjectStateManager.GetObjectStateEntries(EntityState.Modified));
         ObjectStateEntry stateOfFernando =
             data.ObjectStateManager.GetObjectStateEntry(fernando.EntityKey);
         Console.WriteLine("state of Fernando: {0}",
                           stateOfFernando.State.ToString());
         foreach (string modifiedProp in stateOfFernando.GetModifiedProperties())
         {
             Console.WriteLine("modified: {0}", modifiedProp);
             Console.WriteLine("original: {0}", stateOfFernando.OriginalValues[modifiedProp]);
             Console.WriteLine("current: {0}", stateOfFernando.CurrentValues[modifiedProp]);
         }
         int changes = 0;
         try
         {
             changes += data.SaveChanges();
         }
         catch (OptimisticConcurrencyException ex)
         {
             data.Refresh(RefreshMode.ClientWins, ex.StateEntries);
             changes += data.SaveChanges();
         }
         Console.WriteLine("{0} entities changed", changes);
     }
 }
示例#2
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Racers EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToRacers(Racer racer)
 {
     base.AddObject("Racers", racer);
 }