您如何在EF Core中显示基础SQL查询? [英] How do you show underlying SQL query in EF Core?
问题描述
从此"
我在Stack Overflow上看到了许多答案,建议人们使用SQL事件探查器查看基础查询.但是现在我很好奇:您如何做迭戈·维加(Diego Vega)的工作,并使查询显示在应用程序中?
https://docs.microsoft.com/zh-cn/ef/core/miscellaneous/logging
在 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? https://docs.microsoft.com/en-us/ef/core/miscellaneous/logging In the OnConfiguring method of
这篇关于您如何在EF Core中显示基础SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! DbContext
的OnConfiguring方法中,您可以设置记录器,登录控制台是预定义的类型,只需使用 DbContext
you can set your logger, log in console is a predefined type, just use this NuGet. Note that using Factory pattern is a best practice for the logger instances.protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLoggerFactory(MyLoggerFactory) // Warning: Do not create a new ILoggerFactory instance each time
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=EFLogging;Trusted_Connection=True;ConnectRetryCount=0");