How do I view the SQL generated by the Entity Framework?

后端 未结 22 2887
别那么骄傲
别那么骄傲 2020-11-21 05:35

How do I view the SQL generated by entity framework ?

(In my particular case I\'m using the mysql provider - if it matters)

22条回答
  •  失恋的感觉
    2020-11-21 06:07

    EF Core 5.0

    This loooong-awaited feature is available in EF Core 5.0! This is from the weekly status updates:

    var query = context.Set().Where(c => c.City == city);
    Console.WriteLine(query.ToQueryString())
    

    results in this output when using the SQL Server database provider:

    DECLARE p0 nvarchar(4000) = N'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
    

    Notice that declarations for parameters of the correct type are also included in the output. This allows copy/pasting to SQL Server Management Studio, or similar tools, such that the query can be executed for debugging/analysis.

    woohoo!!!

提交回复
热议问题