EntityFramework.Docs icon indicating copy to clipboard operation
EntityFramework.Docs copied to clipboard

Simple way to see SQL statements

Open gzinger opened this issue 5 years ago • 10 comments

In EF6 it was very simple to output the generated SQL with a one-line of code using Database.Logging property. I tried to do everything discussed in this article as well as ASP.NET Core logging linked article, but it doesn't work. Could you please provide a COMPLETE sample of how to see the EF generated SQL in a ASP.NET Core Web API? Thanks.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

gzinger avatar Mar 01 '19 14:03 gzinger

Pl. check this article, may it help you...

http://thedatafarm.com/data-access/logging-in-ef-core-2-2-has-a-simpler-syntax-more-like-asp-net-core/

jayeshdshah avatar Mar 23 '19 14:03 jayeshdshah

It already writes every statement to the logs with loglevel Information.

kwaazaar avatar Nov 21 '19 08:11 kwaazaar

Yes, for the love of god, please simply make it work like it did in EF 6. I thought you were going to do this a long time ago? I would swear this was on the list years ago. The ASP.NET Core logging infrastructure is an absolute train wreck. Not everyone is using this with ASP.NET Core. I had to jump through numerous hoops to get this working in an earlier version of EF Core and now that I have upgraded from EF Core 2.2 to 3.1, it no longer works and I see that the documentation on how to get it to work has changed. This should not be this complicated. Apparently, something on the life cycle of the logging provider changed or something. It works for me on the first instance of the DbContext. Then, when it goes out of scope, logging no longer works for additional instances. The way I've had it working up to this point was to add my provider using the following code in OnModelCreating().

        this.GetService<ILoggerFactory>().AddProvider(new LoggingProvider());

I'm assuming it must have been storing it in a static variable behinds the scene in previous versions of EF and now it isn't.

jemiller0 avatar Dec 21 '19 06:12 jemiller0

This is implemented in EF Core 5! (Even better than in EF6)

ErikEJ avatar Dec 21 '19 07:12 ErikEJ

See https://github.com/dotnet/efcore/issues/19549#issuecomment-572823246 Also document CreateDbCommand: https://github.com/dotnet/efcore/issues/19358

ajcvickers avatar Jan 31 '20 19:01 ajcvickers

Might be worth updating this document to include a note that if you add 1 line to the LogLevel node appsettings.json that you will get EF Core logging: "Microsoft.EntityFrameworkCore.Database.Command": "Information"

Here is the full path to the setting:

{
  "Logging": {
    "LogLevel": {
      "Microsoft.EntityFrameworkCore.Database.Command": "Information"
    }
  }
}

StevenRasmussen avatar Feb 15 '20 16:02 StevenRasmussen

@StevenRasmussen has the right answer. I'll try to get this updated in the next quarter. See this SO thread with 47K views. Several other SO threads on the same question. I was unable to find an official doc on this. See my Answer

Rick-Anderson avatar May 04 '21 02:05 Rick-Anderson

Note: update https://aka.ms/efcore-docs-query-string when this is done.

ajcvickers avatar Aug 25 '21 08:08 ajcvickers

The link to the documentation is broken. In any case, I switched to Dapper since you threw .NET Framework under the bus.

jemiller0 avatar Aug 25 '21 14:08 jemiller0

@jemiller0 The link is in the process of being created.

ajcvickers avatar Aug 25 '21 14:08 ajcvickers