开发者

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?


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜