// See IEntityWrapper documentation public void DetachContext() { if (Context != null && Context.ObjectStateManager.TransactionManager.IsAttachTracking && Context.ObjectStateManager.TransactionManager.OriginalMergeOption == MergeOption.NoTracking) { // If AttachTo() failed while attaching graph retrieved with NoTracking option, // we don't want to clear the Context property of the wrapped entity MergeOption = MergeOption.NoTracking; } else { Context = null; } RelationshipManager.DetachContextFromRelatedEnds(); }