Non-auto-loaded properties are not populated on entities even when explicitly selected by FromSql or LINQ projection
I maintainer di solito rispondono entro 2 giorni
@AndriySvyryd ci sta già lavorando.
Dal 30/10/2026.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 48/100
Direzione di ricerca
Start by tracing EF Core's entity materialization path for properties marked IsAutoLoaded = false, then compare how FromSqlRaw and the LINQ entity-plus-scalar projection select and assign values. The issue includes a runnable repro in Program.cs and reports both paths returning the column while leaving the entity property null. Determine whether that behavior is intentional and add focused regression coverage for the expected behavior.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Description
With a string property configured with Metadata.IsAutoLoaded = false, EF does not populate the property on a materialized entity even when the executed SQL explicitly returns its column.
Two cases:
FromSqlRaw("SELECT [Id], [Content] FROM [#Items]")returns both columns, but the entity'sContentremains null..Select(x => new { Entity = x, x.Content })selects the column and correctly populates the separate projectedContent, butEntity.Contentremains null.
This reproduces with SQL Server and an ordinary string property; no vector types, converters, or third-party EF extensions are involved. Both cases use fresh contexts and AsNoTracking(), so previously tracked entities cannot account for the result.
Full minimal repro
Requires a .NET 11 SDK and SQL Server. The default connection string uses LocalDB; alternatively set the SQLSERVER_CONNECTION environment variable to your SQL Server connection string. Run with dotnet run.
The repro creates only a connection-scoped temporary table in the selected database. It does not create, delete, or change a permanent database/table.
Repro.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer"
Version="11.0.0-rc.2.26504.105" />
</ItemGroup>
</Project>
Program.cs
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
var connectionString = Environment.GetEnvironmentVariable("SQLSERVER_CONNECTION")
?? @"Server=(localdb)\mssqllocaldb;Database=tempdb;Integrated Security=true";
await using var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
await using (var setup = connection.CreateCommand())
{
setup.CommandText = """
CREATE TABLE #Items (Id int PRIMARY KEY, Content nvarchar(100));
INSERT INTO #Items VALUES (1, N'hello');
""";
await setup.ExecuteNonQueryAsync();
}
// Case 1: raw SQL explicitly returns Content.
await using (var ctx = new ReproContext(connection))
{
var query = ctx.Items
.FromSqlRaw("SELECT [Id], [Content] FROM [#Items]")
.AsNoTracking();
Console.WriteLine(query.ToQueryString());
var item = (await query.ToListAsync()).Single();
Console.WriteLine($"FromSql: Id={item.Id}, Content={item.Content ?? "<null>"}");
if (item.Id != 1 || item.Content is not null)
throw new Exception("Case 1 did not reproduce.");
}
// Case 2: LINQ explicitly selects Content alongside the entity.
await using (var ctx = new ReproContext(connection))
{
var query = ctx.Items.AsNoTracking()
.Select(x => new { Entity = x, x.Content });
Console.WriteLine(query.ToQueryString());
var result = (await query.ToListAsync()).Single();
Console.WriteLine(
$"Select: Entity.Id={result.Entity.Id}, Entity.Content={result.Entity.Content ?? "<null>"}, Content={result.Content}");
if (result.Entity.Id != 1 || result.Entity.Content is not null || result.Content != "hello")
throw new Exception("Case 2 did not reproduce.");
}
Console.WriteLine($"Both cases reproduced on {connection.ServerVersion}.");
class ReproContext(SqlConnection connection) : DbContext
{
public DbSet<Item> Items => Set<Item>();
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer(connection);
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Item>().ToTable("#Items");
modelBuilder.Entity<Item>().Property(x => x.Content).Metadata.IsAutoLoaded = false;
}
}
class Item
{
public int Id { get; set; }
public string? Content { get; set; }
}
Actual output
Verified by executing the repro against SQL Server:
SELECT [Id], [Content] FROM [#Items]
FromSql: Id=1, Content=<null>
SELECT [#].[Id], [#].[Content]
FROM [#Items] AS [#]
Select: Entity.Id=1, Entity.Content=<null>, Content=hello
Both cases reproduced on 17.00.4050.
Expected behavior / question
I would expect a non-auto-loaded property to be populated when the query explicitly requests and returns its column, particularly for FromSql, where the SQL is provided by the user. In the LINQ case, the returned column is demonstrably available (result.Content == "hello"), but the entity's property is left unassigned.
Is skipping assignment in these cases intentional, or should the materializer populate the property when the query already returns its column?
Changing the configuration to IsAutoLoaded = true makes both entity properties populate correctly. Explicit scalar-only projections also work; the issue is assignment on materialized entities, not reading the scalar value itself.
Related to #37280, but this is not requesting a new Include/per-query opt-in API: these examples already explicitly select and return the column.
Environment
- EF Core / Microsoft.EntityFrameworkCore.SqlServer:
11.0.0-rc.2.26504.105 - .NET SDK:
11.0.100-rc.2.26504.105 - SQL Server 2025:
17.00.4050, Linux container - Repro client: macOS arm64
The same behavior was also reproduced with EFCore.PG 11 RC2 against PostgreSQL.
- Lingua principale
- C#
- Stelle
- 14.8k
- Fork
- 3.5k
- Merge medio
- 3g 7h
- PR unite (30g)
- 182
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di dotnet/efcore
-
customer-reported
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 2 giorni
-
GroupBy element `OrderBy(...).First()`/`MaxBy` over an owned type mapped to its own table fails to translateForse già presa @cincuranet l’ha presa oggi. Apertaarea-groupby area-query
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
dotnet/efcore#39833 · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
-
SQLite: Polygon.Length translates to GLength, which returns 0Forse già presa @cincuranet l’ha presa oggi. Apertaarea-query area-sqlite
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
dotnet/efcore#39826 · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
-
Migration lock with LockReleaseBehavior.Transaction doesn't prevent concurrent migrations since per-migration transactionsForse già presa @AndriySvyryd l’ha presa 1 giorno fa. Apertaarea-migrations
dotnet/efcore#39824 · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
-
Querying a TPC base type with a JSON complex property throwsForse già presa @cincuranet l’ha presa 1 giorno fa. Apertaarea-query
Difficoltà 4/5 3-5 giorni Idoneità per principianti 22/100
dotnet/efcore#39814 · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di dotnet/efcore
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
SubtitleEdit/subtitleedit#17247 ·
I maintainer di solito rispondono entro 1 giorno
-
Restore test cleanup fails on Windows when restored files are read-onlyForse già presa @JamBalaya56562 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
duplicati/duplicati#8046 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 1 giorno
-
Bundles Documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
pyrevitlabs/pyRevit#4285 ·
I maintainer di solito rispondono entro 1 giorno