如何在 EF Core 中显示底层 SQL 查询? [英] How do you show underlying SQL query in EF Core?
问题描述
在此
我在 Stack Overflow 上看到很多答案表明人们使用 SQL 分析器来查看底层查询.但现在我很好奇:你怎么能像 Diego Vega 所做的那样,让查询直接显示在应用程序中?
.NET 6 更新:EF 日志记录在开发中默认启用.请参阅
参见 Entity Framework Core 的 SQL 日志记录 在官方文档中.这是一个默认情况下不记录的错误,请参阅这个 GitHub 问题.>
At 3:15 from the end of this ".NET Core 2.0 Released!" video, Diego Vega shows a demo of new features in Entity Framework Core 2.0. As part of that, a dump of the underlying SQL is shown in the console app.
I have seen many answers on Stack Overflow suggesting that people use an SQL profiler to view the underlying queries. But now I'm curious: how can you do what Diego Vega did, and get the query to show right there in the application?
Update for .NET 6: EF logging is enabled by default in development. See this GitHub issue
Update: Diego added "Microsoft.EntityFrameworkCore.Database.Command": "Information"
to appsettings.Development.json. See How do you show underlying SQL query in EF Core? for more details.
Just add "Microsoft.EntityFrameworkCore.Database.Command": "Information"
to appsettings.Development.json so it's only logged in dev mode. You typically don't want to log every query in a production app.
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=MyDB-2;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
,"Microsoft.EntityFrameworkCore.Database.Command": "Information"
}
},
"AllowedHosts": "*"
}
The SQL output shows in the command window or VS output window.
See SQL Logging of Entity Framework Core in the official docs. It's a bug that it doesn't log by default, see this GitHub issue.
这篇关于如何在 EF Core 中显示底层 SQL 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!