Skip to content

EF Core 8 & 9 include parameters for "external" primitive variables & collections included in select #35567

Closed as not planned

Description

@ravindUwU

Bug description

Since version 8 and onward, EF core seems to add parameters for primitive typed and primitive collection typed "external" variables that are included in Selects (which is as best as I can describe it 😅; please feel free to reword this issue as necessary).

I've included a repro below in which,

  1. In case 1, only id is selected, as expected.

    SELECT [e].[id] AS [Id]
    FROM [entity] AS [e]
  2. In cases 2, 3 & 4, extra parameters are present in the query when "external" int, List<int> and List<double> variables are included in the select.

    SELECT [e].[id] AS [Id], @__anInt_0 AS [anInt], @__listOfInt_1 AS [listOfInt], @__listOfDouble_2 AS [listOfDouble]
    FROM [entity] AS [e]
  3. This doesn't seem to be an issue in cases 5 & 6, in which a non-primitive variable (anonymous object) is included in the select.

    SELECT [e].[id]
    FROM [entity] AS [e]

I'm noticing that,

  • EF core 7.0.20 selects only the id column and no parameters, as expected.

  • EF core 8.0.12 & 9.0.1 both select the id column as expected, but also include parameters as described above.

Your code

public static async Task Main()
{
	var db = new Db();
	var listOfInt = new List<int> { 1 };
	var listOfDouble = new List<double> { 1d };
	var anInt = 1;
	var anObject = new { };

	Write($"EF Core {typeof(DbContext).Assembly.GetName().Version}");

	Write("\n1. SELECT e.Id");
	await db.Entities.Select((e) => new { e.Id }).ToListAsync();

	Write("\n2. SELECT e.Id, anInt");
	await db.Entities.Select((e) => new { e.Id, anInt }).ToListAsync();

	Write("\n3. SELECT e.Id, anInt, listOfInt");
	await db.Entities.Select((e) => new { e.Id, anInt, listOfInt }).ToListAsync();

	Write("\n4. SELECT e.Id, anInt, listOfInt, listOfDouble");
	await db.Entities.Select((e) => new { e.Id, anInt, listOfInt, listOfDouble }).ToListAsync();

	Write("\n5. SELECT e.Id, anInt, listOfInt, anObject");
	await db.Entities.Select((e) => new { e.Id, anInt, listOfInt, anObject }).ToListAsync();

	Write("\n6. SELECT e.Id, anInt, listOfInt, listOfDouble, anObject");
	await db.Entities.Select((e) => new { e.Id, anInt, listOfInt, listOfDouble, anObject }).ToListAsync();
}

public class Db : DbContext
{
	public DbSet<Entity> Entities { get; init; } = null!;

	protected override void OnConfiguring(DbContextOptionsBuilder options)
	{
		options.UseSqlServer("...");
		options.LogTo(Console.WriteLine, LogLevel.Information);
	}
}

[Table("entity")]
[PrimaryKey(nameof(Id))]
public class Entity
{
	[Column("id")]
	public required int Id { get; set; }
}

public static void Write(string s)
{
	var c = Console.ForegroundColor;
	Console.ForegroundColor = ConsoleColor.Cyan;
	Console.WriteLine(s);
	Console.ForegroundColor = c;
}

Stack traces


Verbose output


EF Core version

9.0.1

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET 9.0.102

Operating system

Windows 11

IDE

Microsoft Visual Studio Professional 2022 x64 17.12.4

Activity

  1. cincuranet commented on Feb 4, 2025

    @cincuranet
    Contributor

    These are good observations. Sending the parameter to server does not break anything, but I agree it is wasteful (and confusing when that happens and when we do client-eval, like with the extra anObject).

    I've created #35579 to track progress on that. There's also #35580, which is why send the parameter to server to evaluate it there in the first place, but apparently fails with local variables.

    I'll close this now and you can follow other issue(s) and see progress there.

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

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions