dale
dale

Reputation: 11

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?

Upvotes: 1

Views: 315

Answers (3)

jkody21
jkody21

Reputation: 126

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/

Upvotes: 0

Craig Stuntz
Craig Stuntz

Reputation: 126547

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();

Upvotes: 2

Mark Seemann
Mark Seemann

Reputation: 233150

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

Upvotes: 1

Related Questions