30

How can I get the sql script generated by an entity framework query? i.e. If I write

entityDataDontext.table1.Where(r => r.primarykey == 1).First();

then how can I get the SQL which should be somewhat like this:

select * from table1 where primarykey = 1

Thanks in advance :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gaurav Pandey
  • 2,798
  • 4
  • 28
  • 41
  • 13
    `var trace = ((ObjectQuery)query).ToTraceString()` – RPM1984 Jan 16 '11 at 00:20
  • 1
    I'd like to add to @RPM1984 comment that to use this against an anonymous type you would use `ObjectQuery` rather than `ObjectQuery`. – The Muffin Man Jun 26 '12 at 22:04
  • Please see duplicates: [How do I view the SQL generated by the entity framework ?](http://stackoverflow.com/questions/1412863/how-do-i-view-the-sql-generated-by-the-entity-framework) [SQL tracing LINQ to Entities](http://stackoverflow.com/questions/137712/sql-tracing-linq-to-entities) – Kip Jan 15 '11 at 16:01

3 Answers3

5

When you're going against SQL Server as your backend database, SQL Server Profiler is always the best tool to see exactly what SQL statements are being sent to your database to be processed.

See Introducting SQL Server Profiler for background info.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
2

I can strongly recommend MiniProfiler: http://miniprofiler.com/

Mariusz.W
  • 1,347
  • 12
  • 19
0

Besides using the SQL Server Profiler, you can try Entity Framework Profiler, which does that and more.

It's paid, with a 30-day trial.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154