当前位置:网站首页>EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
2022-07-31 15:50:00 【极客研究者】
看了下杨中科老师的书,EF Core 5.0很简单的一行代码就可以把生成的SQL输出到控制台中。
查了下微软官方文档:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.LogTo(Console.WriteLine);
但低版本的不适合,我们公司的项目是基于2.2版本的。于是搜寻了下。
有一篇文章可以使用,但是里面的方法已被弃用了,不推荐使用
ConsoleLoggerProvider这个类。
有个老外分享是比较正确的使用姿势:
How to create a LoggerFactory with a ConsoleLoggerProvider?
还是推荐使用依赖注入的方式.
在公司的项目中进行整合:
public void ConfigureServices(IServiceCollection services)
{
.................................
.................................
.................................
.................................
.................................
.................................
.................................
services.AddLogging(builder => builder
.AddConsole()
.AddFilter(level => level >= LogLevel.Information));
}
public class XXXXDbContext
{
private readonly IConfiguration _configuration;
private readonly ILoggerFactory _loggerFactory;
public XXXXDbContext() : this(ConfigurationHelper.Configuration) { }
public InstacartDbContext(IConfiguration configuration)
{
_configuration = configuration;
}
public InstacartDbContext(IConfiguration configuration, ILoggerFactory loggerFactory)
{
_configuration = configuration;
_loggerFactory = loggerFactory;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseLoggerFactory(_loggerFactory).UseSqlServer("ConnectString"));
}
}
}
边栏推荐
- mysql黑窗口~建库建表
- 6-22 Vulnerability exploit - postgresql database password cracking
- Linux查看redis版本(查看mongodb版本)
- i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)
- 更新数据表update
- mongo enters error
- [7.28] Code Source - [Fence Painting] [Appropriate Pairs (Data Enhanced Version)]
- 11 pinia use
- TextBlock控件入门基础工具使用用法,取上法入门
- Applicable Scenarios of Multi-Master Replication (1) - Multi-IDC
猜你喜欢
gerrit中如何切换远程服务器
mysql黑窗口~建库建表
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
C语言”三子棋“升级版(模式选择+AI下棋)
Qt实战案例(54)——利用QPixmap设计图片透明度
WPF project - basic usage of controls entry, you must know XAML
C程序是如何跑起来的01 —— 普通可执行文件的构成
为什么黑客领域几乎一片男生?
Kubernetes principle analysis and practical application manual, too complete
随机推荐
Precautions and solutions when SIGABRT error is reported
update data table update
TextBlock控件入门基础工具使用用法,取上法入门
what exactly is json (c# json)
The normal form of the database (first normal form, second normal form, third normal form, BCNF normal form) "recommended collection"
What is the difference between BI software in the domestic market?
Oracle dynamically registers non-1521 ports
腾讯云部署----DevOps
第二届中国PWA开发者日
MySQL多表联合查询
为什么黑客领域几乎一片男生?
The use of button controls
Emmet 语法
Deployment application life cycle and Pod health check
2.索引及调优篇【mysql高级】
Delete table data or clear table
MySQL的相关问题
Internet banking stolen?This article tells you how to use online banking safely
数据库的范式(第一范式,第二范式,第三范式,BCNF范式)「建议收藏」
border控件的使用