实体框架如何查看SaveChanges方法的SQL语句 [英] Entity Framework How to see SQL statements for SaveChanges method

查看:70
本文介绍了实体框架如何查看SaveChanges方法的SQL语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经使用context.Log来跟踪LINQ到SQL生成的SQL语句,如

I used to use the context.Log for tracing LINQ to SQL generated SQL Statements as shown in Sql Server Query Visualizer – Cannot see generated SQL Query

context.Log = new OutputWindowWriter();

对于EF,是否有类似上述方法的简便方法?

For EF, is there anything similar and easy like the above approach?

推荐答案

通常,您可以通过简单的方法连接内置的跟踪器或任何记录器

In general you can hook up the built-in tracer or any logger by simple

context.Database.Log = msg => Trace.WriteLine(msg);

DpContext构造函数中的

.在 MSDN 中了解更多信息.来自MS的其他一些方法是此处(均基于

in the DbContext constructor. See more in MSDN. Some other approaches from MS are here (all based on DataContext.Log property).

谈论Nate提到的离合器解决方案,它不适用于EF v6(请参见此错误报告).

Talking about the Clutch solution mentioned by Nate, it doesn't work with EF v6 (see this bug-report).

参考

  1. 记录和拦截数据库操作(从EF6开始)
  2. 记录和拦截数据库操作

这篇关于实体框架如何查看SaveChanges方法的SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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