How to look at sql generated by EntityFramework when using Count

When I have a query generated as follows:

var query = from x in Entities.SomeTable
            select x;

I can set a breakpoint and, after hovering over the query, I can see which SQL command will be sent to the database. Unfortunately, I can’t do this when I use Count

var query = (from x in Entities.SomeTable
            select x).Count();

Of course, I could see what was happening with SqlServer using the profiler, but maybe someone knows how to do this (if possible) in VS.

+4
source share
3 answers

You can use ToTraceString():

ObjectQuery<SomeTable> query = (from x in Entities.SomeTable select x).Count();
Console.WriteLine(query.ToTraceString());
0
source

Database.Log , :

using (var context = new MyContext()) 
{ 
    context.Database.Log = Console.Write; 

    // Your code here... 
}

, , ( NLog, Log4Net .net), , .

0

In EF6 and later, you can use the following before a request:

context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);

I found this to be faster than running SQL Profiler and running trace. In addition, this post talks more about this topic: How to view the SQL generated by the Entity Framework?

0
source

Source: https://habr.com/ru/post/1653113/


All Articles