Thursday, January 15, 2015

Entity Framework: Optimizations

Inspect Queries

Use SQL Profiler to see the queries executed by Entity Framework. Optimize DbContext and LINQ queries.

Bulk Updates and Queries

Entity framework can be slow for bulk updates and query. To optimize:
1) Turn off Auto Detect Changes property on DbContext (Can explicitly let EF know what has changed). EF can tie Sprocs to insert, update, delete (instead of generating and executing SQL)
2) Referenced (foreign key) object via an Id property, instead of navigation property (a full instance of referenced object).
3) Do the optimization on the DB Server (views, stored procs, ...)

Connection Resiliency

  • Retry connection upon transient connection failure
  • Use EF class DbExecution strategy: SqlAzureExecutionStrategy, DefaulSqlExecutionStrategy (does nothing). Retry n times, specify max retry period.
  • Configured via DbConfiguration-derived class