Change Information of EF
Majors functions and properties of ObjectContext class:
// // Summary: // Gets the object state manager used by the object context to track object // changes. // // Returns: // The System.Data.Objects.ObjectStateManager used by this System.Data.Objects.ObjectContext. public ObjectStateManager ObjectStateManager { get; }
// // Summary: // Persists all updates to the data source and resets change tracking in the // object context. // // Returns: // The number of objects in an System.Data.EntityState.Added, System.Data.EntityState.Modified, // or System.Data.EntityState.Deleted state when System.Data.Objects.ObjectContext.SaveChanges() // was called. // // Exceptions: // System.Data.OptimisticConcurrencyException: // An optimistic concurrency violation has occurred in the data source. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public int SaveChanges();
Majors functions and properties of ObjectStateManager class:
// Summary: // Occurs when entities are added to or removed from the state manager. public event CollectionChangeEventHandler ObjectStateManagerChanged;
// // Summary: // Returns a collection of System.Data.Objects.ObjectStateEntry objects for // objects or relationships with the given state. // // Parameters: // state: // An System.Data.EntityState used to filter the returned System.Data.Objects.ObjectStateEntry // objects. // // Returns: // A collection of System.Data.Objects.ObjectStateEntry objects in the given // System.Data.EntityState. // // Exceptions: // System.ArgumentException: // When state is System.Data.EntityState.Detached. public IEnumerable<ObjectStateEntry> GetObjectStateEntries(EntityState state);
// // Summary: // Returns an System.Data.Objects.ObjectStateEntry for the object or relationship // entry with the specified key. // // Parameters: // key: // The System.Data.EntityKey. // // Returns: // The corresponding System.Data.Objects.ObjectStateEntry for the given System.Data.EntityKey. // // Exceptions: // System.ArgumentNullException: // When key is null. // // System.ArgumentException: // When the specified key cannot be found in the state manager. // // System.InvalidOperationException: // No entity with the specified System.Data.EntityKey exists in the System.Data.Objects.ObjectStateManager. public ObjectStateEntry GetObjectStateEntry(EntityKey key);
First, a new racer is added with the AddObject() method of the ObjectSet<T> class. This method adds a new entity with the EntityState.
Added information. Next, a racer with the Lastname Alonso is queried. With this entity class, the Starts
property is incremented and thus the entity is marked with the information EntityState.Modified.
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); } } static void DisplayState(string state, IEnumerable<ObjectStateEntry> entries) { foreach (var entry in entries) { var r = entry.Entity as Racer; if (r != null) { Console.WriteLine("{0}: {1}", state, r.Lastname); } } }

浙公网安备 33010602011771号