Common mistakes in EF Core - Jernej Kavka - NDC London 2024

Learn common mistakes and best practices for Entity Framework Core, and discover how to improve performance, handle common issues, and debug problems effectively.

Key takeaways
  • Common mistakes in EF Core include:
    • Using IEnumerable instead of IQueryable in the wrong place
    • Failing to use Include properly, leading to inefficient queries
    • Not specifying database timeout, causing slow performance
    • Not using cancellation tokens, leading to unresponsive applications
    • Failing to paginate queries, causing memory issues
    • Not using async operations, leading to delays
    • Failing to recycle DB contexts, causing connection issues
    • Not using SQL Server’s connection recycling feature
  • Best practices for EF Core include:
    • Using IQueryable instead of IEnumerable whenever possible
    • Using Include properly to avoid inefficient queries
    • Specifying database timeout to improve performance
    • Using cancellation tokens to improve responsiveness
    • Paginating queries to avoid memory issues
    • Using async operations to improve responsiveness
    • Recycling DB contexts to improve connection management
  • Performance considerations:
    • Using the correct data type for columns can improve performance
    • Avoiding unnecessary joins can improve performance
    • Using the AsNoTracking method can improve performance
    • Using the Result method can improve performance
  • Common issues that can occur in EF Core include:
    • Memory issues caused by loading too much data
    • Slow performance caused by inefficient queries
    • Connection issues caused by failing to recycle DB contexts
    • Unresponsive applications caused by not using cancellation tokens
  • Tips for debugging EF Core:
    • Use the EF logger to debug issues
    • Use the SqlServer tool to debug issues
    • Use the dotnet ef command to debug issues
    • Use the EFCore deset command to debug issues