EntityFramework.Docs
EntityFramework.Docs copied to clipboard
Simple way to see SQL statements
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.
- ID: 40a1cdc0-8bda-0188-1231-35c1656ff6d1
- Version Independent ID: 79cfe1f9-1139-c191-8a4b-1697a9f23f23
- Content: Overview of logging and interception - EF Core
- Content Source: entity-framework/core/logging-events-diagnostics/index.md
- Product: entity-framework
- Technology: entity-framework-core
- GitHub Login: @ajcvickers
- Microsoft Alias: avickers
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/
It already writes every statement to the logs with loglevel Information.
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.
This is implemented in EF Core 5! (Even better than in EF6)
See https://github.com/dotnet/efcore/issues/19549#issuecomment-572823246 Also document CreateDbCommand: https://github.com/dotnet/efcore/issues/19358
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 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
Note: update https://aka.ms/efcore-docs-query-string when this is done.
The link to the documentation is broken. In any case, I switched to Dapper since you threw .NET Framework under the bus.
@jemiller0 The link is in the process of being created.