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

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

Closed
ravindUwU opened this issue Feb 1, 2025 · 1 comment
Labels
area-query closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@ravindUwU
Copy link

ravindUwU commented Feb 1, 2025

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

@cincuranet
Copy link
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.

@cincuranet cincuranet added the closed-no-further-action The issue is closed and no further action is planned. label Feb 4, 2025
@cincuranet cincuranet closed this as not planned Won't fix, can't repro, duplicate, stale Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants