Entity Framework Usage Considerations

Entity Framework is Microsoft’s ORM library that enables developers to work with domain-specific objects and eliminates the dependency of SQL queries in a project's data access layer. Entity Framework can also significantly reduce the development time. Visual Studio has good support of EF Wizard and drag-and-drop tools that help generate EF objects without writing a single line of code.

Entity Framework is based on ADO.NET, which means ADO.NET is faster than Entity Framework.

Entity Framework usage considerations

  1. Make sure your database is indexed, as it is a major factor in Entity Framework’s query performance.
  2. Try to fetch only the required number of rows rather than fetching a large number of records.
  3. Reduce the fetch size as much as possible.
  4. You should use stored procedures, functions and compiled query wherever and whenever they are required.
  5. You need to carefully handle the tables with nested relationships or deep hierarchy as Entity Framework performs slower in such cases.
  6. Breaking your EDMX into a module-specific tables model can be a handy approach as well but make sure you handle it carefully as there is a limitation that the Entities from different models cannot be shared even though they refer to the same table in the database.
  7. Use Profilers, and run it when as a part of the unit test to know the query is getting fired as you expect it to trigger.
  8. Entity Framework generates a Select-Where-In-Select style query in case of join and lambda expressions that can be a concern for you as well but you can find them easily by using profilers.
  9. If you are updating records in look please make sure to save it only once instead of saving it multiple times.
  10. If you are fetching records for viewing, then only in that case, you can Disable change tracking for the entity as it is not needed.
  11. Avoid fetching all the fields if not required; try to use data model/entities for the same.
  12. Disable the Change Tracking if it is not required.