Use FirstOrDefault instead of First. This will return null in the face of an empty collection.
IRepository<User> = new UserRepository();
User user = userRepository.FirstOrDefault(u => u.Name == userName);
Use FirstOrDefault instead of First. This will return null in the face of an empty collection.
IRepository<User> = new UserRepository();
User user = userRepository.FirstOrDefault(u => u.Name == userName);