private void ExcludeEntityKey()
        {
            RelationshipEntry relationship = this.ObjectContext.ObjectStateManager.FindRelationship(this.RelationshipSet, new KeyValuePair <string, EntityKey>(this.RelationshipNavigation.From, this.WrappedOwner.EntityKey), new KeyValuePair <string, EntityKey>(this.RelationshipNavigation.To, this.DetachedEntityKey));

            if (relationship == null)
            {
                return;
            }
            relationship.Delete(false);
            if (relationship.State == EntityState.Detached)
            {
                return;
            }
            relationship.AcceptChanges();
        }
        private void ExcludeEntityKey()
        {
            EntityKey ownerKey = WrappedOwner.EntityKey;

            RelationshipEntry relationshipEntry = this.ObjectContext.ObjectStateManager.FindRelationship(RelationshipSet,
                                                                                                         new KeyValuePair <string, EntityKey>(RelationshipNavigation.From, ownerKey),
                                                                                                         new KeyValuePair <string, EntityKey>(RelationshipNavigation.To, DetachedEntityKey));

            // we may have failed in adding the graph before we actually added this relationship, so make sure we actually found one
            if (relationshipEntry != null)
            {
                relationshipEntry.Delete(/*doFixup*/ false);
                // If entry was Added before, it is now Detached, otherwise AcceptChanges to detach it
                if (relationshipEntry.State != EntityState.Detached)
                {
                    relationshipEntry.AcceptChanges();
                }
            }
        }
 internal override void Exclude()
 {
     if (this._wrappedCachedValue.Entity != null)
     {
         TransactionManager transactionManager = this.ObjectContext.ObjectStateManager.TransactionManager;
         bool doFixup = transactionManager.PopulatedEntityReferences.Contains((EntityReference)this);
         bool flag    = transactionManager.AlignedEntityReferences.Contains((EntityReference)this);
         if ((transactionManager.ProcessedEntities == null || !transactionManager.ProcessedEntities.Contains(this._wrappedCachedValue)) && (doFixup || flag))
         {
             RelationshipEntry relationshipEntry = this.IsForeignKey ? (RelationshipEntry)null : this.FindRelationshipEntryInObjectStateManager(this._wrappedCachedValue);
             this.Remove(this._wrappedCachedValue, doFixup, false, false, false, true);
             if (relationshipEntry != null && relationshipEntry.State != EntityState.Detached)
             {
                 relationshipEntry.AcceptChanges();
             }
             if (doFixup)
             {
                 transactionManager.PopulatedEntityReferences.Remove((EntityReference)this);
             }
             else
             {
                 transactionManager.AlignedEntityReferences.Remove((EntityReference)this);
             }
         }
         else
         {
             this.ExcludeEntity(this._wrappedCachedValue);
         }
     }
     else
     {
         if (!(this.DetachedEntityKey != (EntityKey)null))
         {
             return;
         }
         this.ExcludeEntityKey();
     }
 }
        internal override void Exclude()
        {
            Debug.Assert(this.ObjectContext != null, "Should not be trying to remove entities from state manager if context is null");

            if (null != _wrappedCachedValue.Entity)
            {
                // It is possible that _cachedValue was originally null in this graph, but was only set
                // while the graph was being added, if the DetachedEntityKey matched its key. In that case,
                // we only want to clear _cachedValue and delete the relationship entry, but not remove the entity
                // itself from the context.
                TransactionManager transManager = ObjectContext.ObjectStateManager.TransactionManager;
                bool doFullRemove       = transManager.PopulatedEntityReferences.Contains(this);
                bool doRelatedEndRemove = transManager.AlignedEntityReferences.Contains(this);
                // For POCO, if the entity is undergoing snapshot for the first time, then in this step we actually
                // need to really exclude it rather than just disconnecting it.  If we don't, then it has the potential
                // to remain in the context at the end of the rollback process.
                if ((transManager.ProcessedEntities == null || !transManager.ProcessedEntities.Contains(_wrappedCachedValue)) &&
                    (doFullRemove || doRelatedEndRemove))
                {
                    // Retrieve the relationship entry before _cachedValue is set to null during Remove
                    RelationshipEntry relationshipEntry = IsForeignKey ? null : FindRelationshipEntryInObjectStateManager(_wrappedCachedValue);
                    Debug.Assert(IsForeignKey || relationshipEntry != null, "Should have been able to find a valid relationship since _cachedValue is non-null");

                    // Remove the related ends and mark the relationship as deleted, but don't propagate the changes to the target entity itself
                    Remove(_wrappedCachedValue,
                           doFixup: doFullRemove,
                           deleteEntity: false,
                           deleteOwner: false,
                           applyReferentialConstraints: false,
                           preserveForeignKey: true);

                    // The relationship will now either be detached (if it was previously in the Added state), or Deleted (if it was previously Unchanged)
                    // If it's Deleted, we need to AcceptChanges to get rid of it completely
                    if (relationshipEntry != null && relationshipEntry.State != EntityState.Detached)
                    {
                        relationshipEntry.AcceptChanges();
                    }

                    // Since this has been processed, remove it from the list
                    if (doFullRemove)
                    {
                        transManager.PopulatedEntityReferences.Remove(this);
                    }
                    else
                    {
                        transManager.AlignedEntityReferences.Remove(this);
                    }
                }
                else
                {
                    ExcludeEntity(_wrappedCachedValue);
                }
            }
            else if (DetachedEntityKey != null)
            {
                // there may still be relationship entries with stubs that need to be removed
                // this works whether we just added the key entry along with the relationship or if it was already existing
                ExcludeEntityKey();
            }
            // else there is nothing to remove for this relationship
        }