How can I see the SQL generated by EF db.entityset?

I have something like this in a repository:

var results = db.EventSet.Include("Events")
                               .Include("TestResults")
                               .Include("Events.Donors")
                               .Include("Events.DonorPanels")
                               .Include("Events.Locations")
                               .Include("Events.DonorPanels.AgenciesDonors")
                               .Where(x => x.Events.Donors.DonorId == DonorId 
                                      && x.Events.Active)
                               .OrderByDescending(x => x.Events.ScheduledDate)
                               .ThenByDescending(x => x.CreatedAt)
                               .ToList();

How can I see the SQL generated by EF for this?

  • Partition Or Separate Table
  • Converting the name of a day to its integer representation
  • SQL Server auto audit updated column
  • Impact on NHibernate caching for searches with results including calculated value mapped as a formula (e.g. rank)
  • How to join dynamic sql statement in variable with normal statement
  • SqlServer to Postgres DDL migration/conversion
  • change data source dynamically from .net application & pass to connectiong string
  • SQL Server - CAST AND DIVIDE
  • Do ADO.Net DataTables have indexes?
  • What's the best way to calculate similarity between rows in a table based on association?
  • Understanding DataTable with SQL Server DataAdapter for best speed
  • Does this code prevent SQL injection?
  • 3 Solutions collect form web for “How can I see the SQL generated by EF db.entityset?”

    SQL Profiler.

    Alternately, change your code to:

    var q = db.EventSet.Include("Events")
                               .Include("TestResults")
                               .Include("Events.Donors")
                               .Include("Events.DonorPanels")
                               .Include("Events.Locations")
                               .Include("Events.DonorPanels.AgenciesDonors")
                               .Where(x => x.Events.Donors.DonorId == DonorId 
                                      && x.Events.Active)
                               .OrderByDescending(x => x.Events.ScheduledDate)
                               .ThenByDescending(x => x.CreatedAt);
    var sql = (q as ObjectQuery).ToTraceString();
    var results = q.ToList();
    

    One easy way is to use the SQL Server Profiler, but get ready for a nasty surprise.

    Profiler is definitely a great way to go, but if you’re developing against SQL Server express, it’s not available (that I’m aware of).

    Another option would be to use LINQPad. It will show you the SQL, Lambda, IL, and results generated for linq queries. Definitely a must-have tool if you’re doing EF development IMO.

    http://www.linqpad.net/

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.