Reputation: 21108
Just trying to get the sql that is generated by a linq query.
Upvotes: 32
Views: 98329
Reputation: 65988
There are 3 ways do that.
1.You can use LINQPad.It's Free http://www.linqpad.net/
2.You can use SQL Server Profiler inside the Sql Server (Tools --> SQL Server Profiler)
3.You can use Visual Studio Debugger for Generate T-Sql.(with any visual studio version)
Upvotes: 21
Reputation: 1
You can use Diagnostic tools in Visual Studio to see the generated query.
To enable this, go to top right corner, there you can see quick search and type diagnostic tools.
In the results, click on appropriate option and see below for query generated.
Upvotes: 0
Reputation: 872
The easiest way i could suggest is to go with Database
Log
. Put Log
after initializing DataContext
and you will be able to track whatever is done by EF on visual studio output
window.
DataContext db = new DataContext();
db.Database.Log = generatedSQL =>
{
Debug.WriteLine(generatedSQL);
};
Upvotes: 1
Reputation: 52675
Another way
From the MSDN article How to: Display Generated SQL (LINQ to SQL)
Set the DataContext.Log
Property to Console.Out
and you'll see it in the console
Upvotes: 3
Reputation: 16077
With Linq2Sql
dc.GetCommand(query).CommandText
see http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.getcommand.aspx for more info.
But I usually use LinqPad
Upvotes: 33
Reputation: 5283
This popped up on Google, it's an 8-part tutorial. I think it will keep you busy for a few hours, it seems quite detailed to me.
1: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx
3: http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx
4: http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx
Good luck.
Upvotes: 4
Reputation: 48587
Use SQL Profiler
if you are using SQL Server as your database.
Upvotes: 7