0

How can I view the complete sql query generated by Entity Framework (version 5)? I can view it using

query.ToString()

but I don't get all the parameters with it. I get a lot of variables like @p_linq_0.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Brian Kalski
  • 897
  • 9
  • 35
  • Have you put `.EnableSensitiveDataLogging()` on the end of your `UseSqlServer` call in startup? Anlso note that inspecting your queryable's `.DebugView` gives more info than ToString() – Caius Jard Feb 18 '22 at 17:33
  • If you can do `query.ToString()` you are in EF classic. I added the tag. Please choose another tag if you don't agree. – Gert Arnold Feb 18 '22 at 21:22
  • In newer versions see https://blog.oneunicorn.com/2020/01/12/toquerystring/ – Aaron Bertrand Feb 19 '22 at 16:12

1 Answers1

0

You can log the SQL queries in your Console Output window by using EnableSensitiveDataLogging EnableSensitiveDataLogging also log more details when an exception occurs in EF

 public ApplicationDbContext CreateContext()
        => new ApplicationDbContext(
            new DbContextOptionsBuilder<ApplicationDbContext>()
                .UseSqlServer(Configuration["TestDBConnection"])
                .EnableSensitiveDataLogging()
                .Options);

From EF Core 5.0 onwards you can also use the LogTo method which takes an Action delegate as a parameter and enables you to log into the Console or to a file, or to a NoSQL database for example

     public ApplicationDbContext CreateContext()
        => new ApplicationDbContext(
            new DbContextOptionsBuilder<ApplicationDbContext>()
                .UseSqlServer(Configuration["TestDBConnection"])
                .LogTo(Console.WriteLine)
                .Options);
Anton Kovachev
  • 322
  • 3
  • 6