Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 15, 2019

Conversation

ajcvickers
Copy link
Member

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
Copy link
Contributor

ErikEJ commented Dec 15, 2019

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
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
Member 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
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
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
Copy link
Contributor

ErikEJ commented Dec 18, 2019

@Suchiman Great suggestion!

@ajcvickers
Copy link
Member Author

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

@ErikEJ
Copy link
Contributor

ErikEJ commented Dec 19, 2019

@ajcvickers Great news!

@johnnyreilly
Copy link

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

@ErikEJ
Copy link
Contributor

ErikEJ commented Jan 13, 2020

In Daily builds now, will ship in November 2020

@johnnyreilly
Copy link

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
Member 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