Entity Framework CoreMedium

🐞 Async FirstOrDefault Bug

How many issues can you identify?

public async Task<User?> GetUserAsync(int id)
{
    var user = _context.Users
        .FirstOrDefault(u => u.Id == id);

    return user;
}

✅ Explanation

  • The method is async but never awaits.
  • Use FirstOrDefaultAsync().
  • Read-only queries should use AsNoTracking().

💡 Senior Interview Tip

Don't stop at identifying the bug. Explain why it happens, its impact on production, and how you would fix it. That's what interviewers expect from mid-level and senior developers.