refactor usage of EntityManager in commands