What is the best way to see what my EF requests turn into

David Thielen 3,141 Reputation points
2023-06-05T20:29:02.0633333+00:00

Hi all;

What is the best way to see clearly/quickly/easily what an EF query becomes. Both the SQL sent to the DB and any subsequent LINQ operations?

At present I run in the debugger and stop on the line after the query. I then look at the log output and from that guess as to what LINQ operations, if any, followed that.

thanks - dave

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
763 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,606 Reputation points Microsoft Vendor
    2023-06-06T03:10:53.8966667+00:00

    @David Thielen, Welcome to Microsoft Q&A, you could use UseLoggerFactory to see the generated SQL Query.

    Please install Nuget-Package Microsoft.Extensions.Logging.Console first of all.

    Then, you could add the following code in your OnConfiguring method:

     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
    
            optionsBuilder.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()))
                 .EnableSensitiveDataLogging();
        }
    
    

    Based on my test, it could generate the sql query in the console.

    enter image description here

    Hope my solution could be helpful for you.

    Best Regards,

    Jack

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.