Skip to content

ToQueryString with Table Valued Parameter is missing the type name #33849

Closed

Description

@Charlieface

When using Table Valued Parameters in SQL Server, an attempt to use .ToQueryString() generates the following rather strange and invalid line for the parameter.

DECLARE @p1 structured = ;

Use case

Using TVPs is a common usage to be able to pass in large amounts of data into SQL Server efficiently. A common way of doing this would be to create a raw SQL query, and looks something like the following:

var table = new DataTable { Columns = {
    { "Value", typeof(int) },
} };
foreach (var someObject in someList)
    table.Add(someObject.Id);

var tvp = Db.Database.FromSqlQuery<int>($"SELECT * FROM {new SqlParameter("@data", table) { TypeName = "dbo.IntList" }}");

You can then compose over that in the normal way:

var query = (
    from e in Db.Employee
    where tvp.Contains(e.Id)
);

The bug

But if you try to use .ToQueryString on those expressions, you get incorrect, non-compilable SQL.

DECLARE @p1 structured = ;

I understand the docs say

This string may not be suitable for direct execution and is intended only for use in debugging.

But in this case it's not difficult to fix.

The fix

It seems difficult, inefficient (and probably counter-productive in the case of IEnumerable<SqlDataRecord> parameter values) to actually generate INSERT rows for the data, so we shouldn't worry about that. It's enough to just generate the type name correctly, without the spurious =. This would still allow the code to be pasted straight into apps such as SSMS to generate query plans etc.

In SqlServerQueryStringFactory.cs, we need to change the code at:

builder
.Append("DECLARE ")
.Append(parameter.ParameterName)
.Append(' ')
.Append(typeName)
.Append(" = ");
if (parameter.Value == DBNull.Value || parameter.Value is null)

To

            builder
                .Append("DECLARE ")
                .Append(parameter.ParameterName)
                .Append(' ')
                .Append(typeName);

            if (parameter.SqlDbType != SqlDbType.Structured)
            {
                builder.Append(" = ");
                if (parameter.Value == DBNull.Value || parameter.Value is null)

And the code at:

SqlDbType.Structured => builder.Append("structured"),

To:

                SqlDbType.Structured => builder.Append(sqlParameter.TypeName),

EF Core version: 7.0.14
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows 11
IDE: LinqPad

Activity

  1. assigned and unassigned on May 30, 2024
  2. roji commented on May 30, 2024

    @roji
    Member

    @ajcvickers you're the "ToQueryString" expert, right? :)

  3. ajcvickers commented on May 30, 2024

    @ajcvickers
    Contributor

    @roji I guess since I wrote it I must be. ;-)

  4. added this to the Backlog milestone on Jun 4, 2024
  5. removed their assignment
    on Aug 31, 2024
  6. added theissue type on Jun 15, 2025
  7. Charlieface commented on Nov 24, 2025

    @Charlieface
    Author

    @ajcvickers Are you able to push this bugfix forward? It seems very simple, and not likely affect anyone, given ToQueryString is for debugging anyway.

  8. roji commented on Nov 24, 2025

    @roji
    Member

    @Charlieface this issue is in the backlog and has received zero votes, so we're not prioritizing it. Having said that, if you're interested in doing the work and submitting a PR, go ahead and do so.

  9. added a commit that references this issue on Jun 9, 2026
    2e03f72
  10. added a commit that references this issue on Jun 9, 2026
    0925821
  11. modified the milestones: Backlog, 11.0.0 on Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions