Bug description
The cosmos provider translates anonymous or dto projections with a single property as SELECT VALUE prop causing implicit undefined filtering behavior.
Anonymous or dto projections with multiple properties get translated as SELECT prop1, prop2, throwing an 'nullable object must have a value' exception in the shaper for undefined results.
Also see: #34067 (comment)
Your code
using Microsoft.EntityFrameworkCore;
var options = new DbContextOptionsBuilder<MyDbContext>()
.EnableSensitiveDataLogging()
.UseCosmos("AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", "test")
.LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Information)
.Options;
using var context = new MyDbContext(options);
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.Entities.Add(new Entity());
await context.SaveChangesAsync();
var noResults = await context.Entities.Select(x => new { x.Associate!.NestedAssociate!.Id }).ToListAsync();
var noResultsDto = await context.Entities.Select(x => new Dto { Id = x.Associate!.NestedAssociate!.Id }).ToListAsync();
var exceptionInShaper = await context.Entities.Select(x => new { x.Associate!.NestedAssociate!.Id, x.Associate.NestedAssociate.String }).ToListAsync();
var exceptionInShaperDto = await context.Entities.Select(x => new Dto { Id = x.Associate!.NestedAssociate!.Id, String = x.Associate.NestedAssociate.String }).ToListAsync();
public class MyDbContext(DbContextOptions options) : DbContext(options)
{
public DbSet<Entity> Entities { get; set; } = null!;
}
public class Entity
{
public Guid Id { get; set; }
public AssociateEntity? Associate { get; set; }
}
public class AssociateEntity
{
public Guid Id { get; set; }
public NestedAssociateEntity? NestedAssociate { get; set; } = null!;
}
public class NestedAssociateEntity
{
public Guid Id { get; set; }
public string? String { get; set; }
}
public class Dto
{
public Guid Id { get; set; }
public string? String { get; set; }
}
Stack traces
An exception occurred while iterating over the results of a query for context type 'MyDbContext'.
System.InvalidOperationException: Nullable object must have a value.
at lambda_method153(Closure, QueryContext, JToken)
at Microsoft.EntityFrameworkCore.Cosmos.Query.Internal.CosmosShapedQueryCompilingExpressionVisitor.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
Verbose output
info: 19-May-26 13:33:18.651 CosmosEventId.ExecutingSqlQuery[30100] (Microsoft.EntityFrameworkCore.Database.Command)
Executing SQL query for container 'MyDbContext' in partition 'None' [Parameters=[]]
SELECT VALUE c["Associate"]["NestedAssociate"]["Id"]
FROM root c
info: 19-May-26 13:33:18.798 CosmosEventId.ExecutedReadNext[30102] (Microsoft.EntityFrameworkCore.Database.Command)
Executed ReadNext (129.3505 ms, 2.89 RU) ActivityId='bf5132a2-f8d4-4aa1-9941-5adbaf9e97e7', Container='MyDbContext', Partition='None', Parameters=[]
SELECT VALUE c["Associate"]["NestedAssociate"]["Id"]
FROM root c
info: 19-May-26 13:33:18.808 CosmosEventId.ExecutingSqlQuery[30100] (Microsoft.EntityFrameworkCore.Database.Command)
Executing SQL query for container 'MyDbContext' in partition 'None' [Parameters=[]]
SELECT VALUE c["Associate"]["NestedAssociate"]["Id"]
FROM root c
info: 19-May-26 13:33:18.812 CosmosEventId.ExecutedReadNext[30102] (Microsoft.EntityFrameworkCore.Database.Command)
Executed ReadNext (3.345 ms, 2.89 RU) ActivityId='2a8bfca9-b3c3-4c89-8077-76261e3ff1af', Container='MyDbContext', Partition='None', Parameters=[]
SELECT VALUE c["Associate"]["NestedAssociate"]["Id"]
FROM root c
info: 19-May-26 13:33:23.621 CosmosEventId.ExecutingSqlQuery[30100] (Microsoft.EntityFrameworkCore.Database.Command)
Executing SQL query for container 'MyDbContext' in partition 'None' [Parameters=[]]
SELECT c["Associate"]["NestedAssociate"]["Id"], c["Associate"]["NestedAssociate"]["String"]
FROM root c
info: 19-May-26 13:33:23.650 CosmosEventId.ExecutedReadNext[30102] (Microsoft.EntityFrameworkCore.Database.Command)
Executed ReadNext (29.1839 ms, 2.27 RU) ActivityId='8d688ec0-69b7-4827-9263-49bda1304f3d', Container='MyDbContext', Partition='None', Parameters=[]
SELECT c["Associate"]["NestedAssociate"]["Id"], c["Associate"]["NestedAssociate"]["String"]
FROM root c
EF Core version
10.0.8
Database provider
Microsoft.EntityFrameworkCore.Cosmos
Target framework
.NET 10
Operating system
W11
IDE
VS2026
Bug description
The cosmos provider translates anonymous or dto projections with a single property as
SELECT VALUE propcausing implicit undefined filtering behavior.Anonymous or dto projections with multiple properties get translated as
SELECT prop1, prop2, throwing an 'nullable object must have a value' exception in the shaper for undefined results.Also see: #34067 (comment)
Your code
Stack traces
Verbose output
EF Core version
10.0.8
Database provider
Microsoft.EntityFrameworkCore.Cosmos
Target framework
.NET 10
Operating system
W11
IDE
VS2026