Skip to content

DebugView for query and simple way to get SQL from an EF LINQ query - #19324

Merged
ajcvickers merged 1 commit into
masterfrom
ShowMeTheSql1214
Dec 15, 2019
Merged

ajcvickers merged 1 commit into
masterfrom
ShowMeTheSql1214

Conversation

@ajcvickers

Copy link
Copy Markdown
Contributor

Fixes #6482

Given a query like:

var query = context.Set<Customer>().Where(c => c.City == city);

The SQL that will be generated for this query can be obtained by:

var queryString = query.ToQueryString();

and looks like this:

-- @__city_0='London' (Size = 4000)
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[City] = @__city_0

Also, this is used to provide a DebugView with both the nicely printed expression:

image

and the generated SQL:

image

Fixes #6482

Given a query like:
```C#
var query = context.Set<Customer>().Where(c => c.City == city);
```

The SQL that will be generated for this query can be obtained by:

```C#
var queryString = query.ToQueryString();
```

and looks like this:
```sql
-- @__city_0='London' (Size = 4000)
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[City] = @__city_0
```

Also, this is used to provide a `DebugView` with both the nicely printed expression:

and the generated SQL:
@ErikEJ

ErikEJ commented Dec 15, 2019 •

Copy link
Copy Markdown
Contributor

Yay! Maybe this time?

@ajcvickers
ajcvickers merged commit f8ac8b1 into master Dec 15, 2019
@ajcvickers
ajcvickers deleted the ShowMeTheSql1214 branch December 15, 2019 22:15
public string ToQueryString()
{
var sqlQuery = _querySqlGeneratorFactory.Create().GetSqlQuery(
(SelectExpression)new InExpressionValuesExpandingExpressionVisitor(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract and DRY as this is likely to change in the future.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did that, but then reverted since it was more lines of code. But will do it since you think it's worth it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fewer LoC isn't always better 😃

ajcvickers added a commit that referenced this pull request Dec 16, 2019
@ajcvickers ajcvickers mentioned this pull request Dec 16, 2019
ajcvickers added a commit that referenced this pull request Dec 16, 2019
@Suchiman

Copy link
Copy Markdown
Contributor

Would it be possible to get

DECLARE @__city_0 nvarchar(4000) = 'London';
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[City] = @__city_0

instead of

-- @__city_0='London' (Size = 4000)
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[City] = @__city_0

Most often when i look for the query string, i want to copy paste it into SSMS to get an actual execution plan which requires manual fixup of the parameters.

@ErikEJ

ErikEJ commented Dec 18, 2019

Copy link
Copy Markdown
Contributor

@Suchiman Great suggestion!

@ajcvickers

Copy link
Copy Markdown
Contributor Author

@Suchiman @ErikEJ We discussed today--see #19334 (comment)

@ErikEJ

ErikEJ commented Dec 19, 2019

Copy link
Copy Markdown
Contributor

@ajcvickers Great news!

@johnnyreilly

Copy link
Copy Markdown

This looks amazing! Are there any thoughts as to when this will ship?

@ErikEJ

ErikEJ commented Jan 13, 2020

Copy link
Copy Markdown
Contributor

In Daily builds now, will ship in November 2020

@johnnyreilly

Copy link
Copy Markdown

So pleased to hear it! Out curiousity, how come it's not going out until November? That's a little while from now. Is it part of the next major release of EF core?

@ajcvickers

Copy link
Copy Markdown
Contributor Author

@johnnyreilly For business reasons, the EF Core release schedule aligns with that of .NET Core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a simple way to get SQL statements generated from EF queries

6 participants