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?

  • Delete specific values from column with where condition?
  • SQL query - Join that returns the first two records of joining table
  • How can I copy-and-paste Data into MySQL Workbench table?
  • How do you call a Stored Procedure in SSIS?
  • Sql Server Error Caused by Web Part
  • java code make a stored procedure return a resultset in DAO layer
  • Access to .NET application variable in CLR trigger
  • Executing stored proc from DotNet takes very long but in SSMS it is immediate
  • Concise/elegant method for duplicating a table's schema?
  • Wrong DateTime parsing when using other cultures
  • Deploying SQL CLR Project fails when creating assembly on database
  • sql connection string from AD account in web.config
  • 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.