title: Entity Framework and SQL Server Profiler
body: <p>I have some performance measuring issue between the <strong>EF query</strong> run through the web application and running the Profiler generated <strong>T-SQL directly into the SQL Query window</strong>.</p>
  
  <p>Following is my EF query that executes through the web application:</p>
  
  <pre><code>IEnumerable&lt;application&gt; _entityList = context.applications
                      .Include(context.indb_generalInfo.EntitySet.Name)
                      .Include(context.setup_budget.EntitySet.Name)
                      .Include(context.setup_committee.EntitySet.Name)
                      .Include(context.setup_fund.EntitySet.Name)
                      .Include(context.setup_appStatus.EntitySet.Name)
                      .Include(context.appSancAdvices.EntitySet.Name)
                      .Where(e =&gt; e.indb_generalInfo != null);
  
                  if (isIFL != null)
                      _entityList = _entityList.Where(e =&gt; e.app_isIFL == isIFL);
  
                  int _entityCount = _entityList.Count(); // hits the database server at this line
  </code></pre>
  
  <p>While tracing the above EF Query in SQL Profiler it reveals that it took around <strong>221'095 ms</strong> to execute. (The applications table having 30,000+, indb_generalInfo having 11,000+ and appSancAdvices having 30,000+ records).</p>
  
  <p>However, when I copy the T-SQL from Profiler and run it directly from Query window it takes around <strong>4'000 ms</strong> only.</p>
  
  <p>Why is it so?</p>