Simple record, the original in: http://www.dotnetjalps.com/2012/12/Where-I-can-find-SQL-Generated-by-Entity-framework.html
1,Monitor SQL statements generated by EF
using System;using System.Runtime.CompilerServices;using System.Linq;using System.Data; namespace EntityframeworkSQL{ class Program { static void Main(string[] args) { using (CustomerEntities customerEntities = new CustomerEntities()) { var customerNames = from c in customerEntities.Customers select c.CustomerName; string sql = ((System.Data.Objects.ObjectQuery)customerNames).ToTraceString(); Console.WriteLine(sql); } } }}
2,Monitor the SQL statements produced by LINQ to SQL
using System;using System.Linq;using System.Data; namespace LinqToSQL{ class Program { static void Main(string[] args) { using (CustomerDataContext customerContext = new CustomerDataContext()) { var customerNames = from c in customerContext.Customers select c.CustomerName; string sql = customerContext.GetCommand(customerNames).CommandText; Console.WriteLine(sql); } } }}