当前位置:网站首页>通过代码查看EFCore的SQL语句
通过代码查看EFCore的SQL语句
2022-08-07 14:53:00 【算盘】
方法1:标准日志
class MyDbContext : DbContext
{
public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder=>{builder.AddConsole();});
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLoggerFactory(ILoggerFactory);
}
}
方法2:简单日志
class MyDbContext : DbContext
{
optionsBuilder.LogTo(msg =>
{
if (!msg.Contains("CommandExecuting")) return;
Console.WriteLine(msg);
});
}
方法3:TOQUERYSTRING
需要引用:using Microsoft.EntityFrameworkCore;
1、上面两种方式无法直接得到一个操作的SQL语句,而且在操作很多的情况下,容易混乱
2、EF Core的Where方法返回的是IQueryable类型,DbSet也实现了IQueryable接口。IQueryable有扩展方法ToQueryString()可以获得SQL
3、不需要真的执行查询才获取SQL语句;只能获取查询操作的
写测试性代码,用简单日志;
正式需要记录SQL给审核人员或者排查故障,用标准日志;
开发阶段,从繁杂的查询操作中立即看到SQL,用ToQueryString()
边栏推荐
- LeetCode Hot Questions HOT 100 (1. Sum of two numbers)
- C Expert Programming Chapter 7 Thinking About Memory 7.8 Take it easy --- "Thing King" and "Page Game"
- 【HCIP】BGP 选路问题小型实验
- [Advanced Mathematics] Advanced Number Arrangement: Common Equivalent Infinitesimals, Derivatives and Differentials, Differential Equations
- Getting started with shell scripting
- Programming Experts in C Chapter 8 Why Programmers Can't Tell the Difference Between Halloween and Christmas 8.9 How and Why Casting
- 锁相环工作原理
- mysql主从同步在master机上为slave机添加一同步帐号问题。
- 二、工业方案推荐系统
- C专家编程 第8章 为什么程序员无法分清万圣节和圣诞节 8.9 如何进行强制类型转换,为何要进行类型强制转换
猜你喜欢

ADC外部RC电路电阻和电容选取计算方法

LeetCode Hot Question HOT 100 (7. The container that holds the most water)

004_Eureka注册中心

嵌入式设备参数存储技巧

桥接模式:Swift 实现

LeetCode hot topic HOT 100 (10. Delete the Nth node from the bottom of the linked list)

Flink CDC
![[Principle of Database System] Chapter 3 BC Normal Form, Third Normal Form and Fourth Normal Form](/img/1a/e97745e62287fcf494d1479deaa632.png)
[Principle of Database System] Chapter 3 BC Normal Form, Third Normal Form and Fourth Normal Form

66:第五章:开发admin管理服务:19:开发【查看用户详情,接口】;

LeetCode 热题 HOT 100(7.盛最多水的容器)
随机推荐
Hash table | The sum of three numbers, the sum of four numbers | The most suitable `double pointer method` | leecode brush notes
嵌入式开发:嵌入式基础–了解微控制器引导过程
微信小程序——点击事件 bindtap和catchtap的区别
Niu Ke Interview High Frequency List (Group 2) Difficulty: Easy & Moderate
国内软件测试岗,可以卷到什么程度?
LeetCode HOT HOT 100 (5. Back to the longest text string)
想交易场内基金去哪个证券公司开户更快更安全
65:第五章:开发admin管理服务:18:开发【根据条件,分页查询用户列表,接口】;(使用了Converter转换器,把url中String格式的日期,转成Date格式的;)
MethodInterceptor
文件管理:文件存储空间管理
[21天学习挑战赛——内核笔记](三)——Pinctrl介绍
基于TCP的聊天系统
06 【泛型】
2022牛客多校六 A-Array(构造+哈夫曼)
基于RK3566中RTL8201F网口百兆调试笔记
AQS同步组件-Semaphore(信号量)解析和案例
Chapter 8 C programming expert why programmers cannot distinguish where Halloween and Christmas 8.5 prototype fail
微信自动发卡机器人说明
浏览器工作原理与实践学习笔记(一)宏观视角下的浏览器
网页模板 pug 基本语法