public void DeleteFromRepository(WarehouseLocation item) { if (_repositoryContext.GetEntityDescriptor(item) != null) { //if it exists in the db delete it from the db WarehouseEntities context = new WarehouseEntities(_rootUri); context.MergeOption = MergeOption.AppendOnly; context.IgnoreResourceNotFoundException = true; WarehouseLocation deletedWarehouseLocation = (from q in context.WarehouseLocations where q.WarehouseLocationID == item.WarehouseLocationID select q).FirstOrDefault(); if (deletedWarehouseLocation != null) { context.DeleteObject(deletedWarehouseLocation); context.SaveChanges(); } context = null; _repositoryContext.MergeOption = MergeOption.AppendOnly; //if it is being tracked remove it... if (GetWarehouseLocationEntityState(item) != EntityStates.Detached) { _repositoryContext.Detach(item); } } }