实体框架如何查看SaveChanges方法的SQL语句 [英] Entity Framework How to see SQL statements for SaveChanges method
问题描述
我曾经使用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).
参考
这篇关于实体框架如何查看SaveChanges方法的SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!