您如何在EF Core中显示基础SQL查询? [英] How do you show underlying SQL query in EF Core?

查看:65
本文介绍了您如何在EF Core中显示基础SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从此"

我在Stack Overflow上看到了许多答案,建议人们使用SQL事件探查器查看基础查询.但是现在我很好奇:您如何做迭戈·维加(Diego Vega)的工作,并使查询显示在应用程序中?

解决方案

https://docs.microsoft.com/zh-cn/ef/core/miscellaneous/logging

DbContext 的OnConfiguring方法中,您可以设置记录器,登录控制台是预定义的类型,只需使用

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 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");

这篇关于您如何在EF Core中显示基础SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆