Repository navigation
Closed as not planned
Closed as not planned
Copy link
Description
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,
-
In case 1, only
idis selected, as expected.SELECT [e].[id] AS [Id] FROM [entity] AS [e]
-
In cases 2, 3 & 4, extra parameters are present in the query when "external"
int,List<int>andList<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]
-
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,
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
cincuranet commentedon Feb 4, 2025
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.