public void Detach(string logicalName, Guid?id) { if (this.context != null) { var candidates = (from c in this.context.GetAttachedEntities() where c.LogicalName == logicalName select c); if (id != null) { candidates = (from c in candidates where c.Id == id.Value select c); } foreach (var r in candidates.ToArray()) { context.Detach(r); } } }