The error message is perfectly clear - there is no
SingleOrDefaultAsync
extension method for the
IEnumerable<T>
interface. The data is already in memory, so there's nothing for an async method to wait for.
Your repository is badly designed. Rather than loading the entire database into memory just to find a single record, you should add a method to your repository to load just the record you're looking for:
public interface IRepositoryTab<T> where T : class
{
IEnumerable<T> GetAll();
Task<T> SingleOrDefaultAsync(Expression<Func<T, bool>> predicate);
}
public class RepositoryTab<T> : IRepositoryTab<T> where T : class
{
...
public Task<T> SingleOrDefaultAsync(Expression<Func<T, bool>> predicate)
{
return dbSet.SingleOrDefaultAsync(predicate);
}
}
...
public async Task<IActionResult> Create(int? nextID)
{
Employee emp = await _repository.SingleOrDefaultAsync(m => m.EmployeeId == NextVal);
}
NB: This is still a leaky abstraction, since the predicate you pass in must be convertible to a database query. This is where generic repositories fall down; a concrete repository would provide a method to pass in the entity's primary key instead. But with a generic repository, where you don't know what type the primary key will be, you can't easily do that.