Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profile EntityFramework

I have the following sample code:

  Context context = new Context();
  Repository repository = new Repository(context);

  Post post = repository.First<Post>(x => x.Id == 1);

  Model model = new Model {
    Created = cube.Created,
    Id = cube.Id,
    Name = cube.Name,
    Pack = cube.Pack.Id,
    Mimes = context.Files.Where(x => x.Id == 1).Select(x => x.Mime).ToList()
  };

I need to find which SQL queries are being sent to the database.

How can I profile the EF queries using SQL Express and VS 2012?

Is there any tool for this?

like image 860
Miguel Moura Avatar asked Dec 11 '12 01:12

Miguel Moura


2 Answers

The EF Profiler specified in the other answer is a beast - wildly powerful, but that doesn't come for free ($$, disk space and time). It's also made by Oren Eini of NHibernate and RavenDB fame.

The tools I like to use in a pinch are way, way more light-weight, and of course can't begin to compare to EF Prof in terms of features - but the cost ($$, time and disk) is, or approaches, zero.

I tested these lightweight profilers with EF on LocalDb, but their main use cases are the more traditional Sql Server flavors (including Express).

ExpressProfiler

By far the easiest to use one (and also the most bare-bones-show-me-the-SQL-statements-kthxbye) is ExpressProfiler on CodePlex.

enter image description here

And the whole program is a single 126 KB EXE ! Now that's lightweight!

AnjLab Sql Profiler

The other one is the one from DataWizard, which used to be free (apparently prices now start at $5).

Somebody managed to save a snapshot on GitHub (including xcopy-installable binaries) when it was open-source.

The executable presents itself as "AnjLab Sql Profiler" and allows some filtering of the displayed events - but this strength is also its weakness, as there are sooo many event types that can be enabled/disabled. If everything is selected, the output is overwhelmingly verbose.

A saner output can be obtain by only selecting specific events.

enter image description here

Here are the events I have selected in order to get the above output:

enter image description here

like image 180
Cristian Diaconescu Avatar answered Oct 21 '22 00:10

Cristian Diaconescu


You have a few options here.

  1. Examine the query by printing it at runtime using ((ObjectQuery)query).ToTraceString() see https://stackoverflow.com/a/7901917/1070291 (I think this only works for queries so may not be the best in the above scenario)
  2. Use SQL Profiler which is built into management studio
  3. Use Entity Framework Profiler (a very good third party tool with a trial version, this is what I use to analyse queries)
like image 24
Not loved Avatar answered Oct 21 '22 00:10

Not loved