Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check LINQ query against SQL Server database

Is there any way to in .NET to see what LINQ query against database we are firing? For eg. I am wring a query in LINQ and I want to see that what SQL query is firing to communicate with database.

Is there any Visual Studio window or any other way?

like image 363
Parveen Avatar asked Mar 30 '15 11:03

Parveen


2 Answers

Are you looking for something like this

var context = new MyContext();
context.Database.Log = s => Debug.WriteLine(s);

Then whenever a query is executed, you'll see an output like :

var customers = context.Customers.ToList();

Opened connection at 30-3-2015 13:48:03 +02:00

SELECT [Extent1].[Guid] AS [Guid],
[Extent1].[FirstName] AS [FirstName],
[Extent1].[LastName] AS [LastName],
[Extent1].[Email] AS [Email],
[Extent1].[Created] AS [Created]
FROM [dbo].[Customer] AS [Extent1]

-- Executing at 30-3-2015 13:48:03 +02:00

-- Completed in 0 ms with result: SqlDataReader

Closed connection at 30-3-2015 13:48:03 +02:00

like image 179
Szeki Avatar answered Sep 18 '22 13:09

Szeki


You can use SQL Profiler to see how the LINQ expression is translated into SQL statement.

like image 28
Burak Karakuş Avatar answered Sep 17 '22 13:09

Burak Karakuş