Optimized some code in EntityManagement.