Home

SQL generated by Linq doesn't include the parameter values (SQL tab)

edited January 5

When running a linq query that contains parameters, the generated query doesn't show the parameter values used.

This Linq
var x = Guid.Parse("a34e9117-c95e-4b96-8fa0-0002016d915c");
var tasks = Tasks.IgnoreQueryFilters().Where(t=> t.Id == x).Select(t => t.Id).Dump();

Generates this SQL:
-- Region Parameters
-- @__x_0='?' (DbType = Guid)
-- EndRegion
SELECT [t].[Id]
FROM [Tasks] AS [t]
WHERE [t].[Id] = @__x_0
GO

Notice that the region parameter values are set to '?' and don't show the value used (not that the query would work anyway).

Some other details that may be important:

  • I've experienced this when connecting to EF Core contexts (using the dll) against both SQL and Postgres Dbs
  • SQL generated by Postgres Linq To Db connection not only has proper param values, but produces SQL that could be run without altering

Comments

  • Follow up question:
    Even if the parameters were shown, it's not like the query could be run as-is. Is there something I'm missing in the configuration that would generate SQL that could be copy/pasted into SSMS and run?

  • It's designed to be pastable only with the LINQ-to-SQL driver (and even then, there are edge-cases when it might not work, such as when the parameter values are very long). With EF Core, LINQPad intercepts commands by implementing ILogger and there are limitations on what data it exposes.

    Regarding why your parameter values are not showing, it could be that you need to add optionsBuilder.EnableSensitiveDataLogging(true) to your context's OnConfiguring method. See here for an example:
    https://linqpad.azureedge.net/public/SampleDbContext.cs

Sign In or Register to comment.