-1

A simple search on getting generated SQL by EF6 returns lots of results, even many in Stackoverflow. but none of them has suggested any ways when method chaining is used.

For example(How do I view the SQL generated by the Entity Framework?):

IQueryable query = from x in appEntities
             where x.id = 32
             select x;

var sql = ((System.Data.Objects.ObjectQuery)query).ToTraceString();

but I want to get the generated sql query if it was used like:

appEntities.x.Where(x.id==32);
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171

1 Answers1

3

You can use Diagnostic/Profiling Tools

Debug -> Windows -> Show Diagnostic Tools ( Ctrl+Alt+F2)

inside Visual Studio, then, while debugging, go to Events tab and click on ADO.NET event (or just hover), which will contain the actual sql string queried to DB.

Or you can add this line

dbContext.Database.Log = s => Debug.WriteLine(s);

before your queries and it will print out your queries inside Output -> Debug.

Or store inside some string if you prefer:

//TODO: Remove after checking
StringBuilder log = new StringBuilder();
dbContext.Database.Log = s => log.Append(s);
SᴇM
  • 7,024
  • 3
  • 24
  • 41