/// <summary> /// Called by the <see cref="ObservableCollection{T}" /> base class when the collection changes. /// This method looks at the change made to the collection and reflects those changes in the /// state manager. /// </summary> /// <param name="e"> /// The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs" /> instance containing the event data. /// </param> protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { Debug.Assert( e.Action != NotifyCollectionChangedAction.Reset, "Should not get Reset event from our derived implementation of ObservableCollection."); // Avoid recursively reacting to changes made to this list while already processing state manager changes. // That is, the ObservableCollection only changed because we made a change based on the state manager. // We therefore don't want to try to repeat that change in the state manager. if (!_inStateManagerChanged && _internalContext != null) { if (e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Replace) { foreach (TEntity entity in e.OldItems) { _internalContext.Set <TEntity>().Remove(entity); } } if (e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Replace) { foreach (TEntity entity in e.NewItems) { // For something that is already in the state manager as Unchanged or Modified we don't try // to Add it again since doing so would change its state to Added, which is probably not what // was wanted in this case. if (!_internalContext.EntityInContextAndNotDeleted(entity)) { _internalContext.Set <TEntity>().Add(entity); } } } } base.OnCollectionChanged(e); }
/// <summary> /// Initializes a new instance of the <see cref = "InternalEntityEntry" /> class for an /// entity which may or may not be attached to the context. /// </summary> /// <param name = "internalContext">The internal context.</param> /// <param name = "entity">The entity.</param> public InternalEntityEntry(InternalContext internalContext, object entity) { //Contract.Requires(internalContext != null); //Contract.Requires(entity != null); _internalContext = internalContext; _entity = entity; _entityType = ObjectContextTypeCache.GetObjectType(_entity.GetType()); _stateEntry = _internalContext.GetStateEntry(entity); if (_stateEntry == null) { // This will cause the context and model to be initialized and will throw an exception // if the entity type is not part of the model. _internalContext.Set(_entityType).InternalSet.Initialize(); } }
public virtual DbSet Set(Type entityType) { Check.NotNull(entityType, "entityType"); return((DbSet)InternalContext.Set(entityType)); }
public virtual DbSet <TEntity> Set <TEntity>() where TEntity : class { return((DbSet <TEntity>)InternalContext.Set <TEntity>()); }