Description:
When attempting to project a query result from Cosmos DB using Entity Framework Core, a System.NullReferenceException occurs with the stack trace pointing to lambda_method6.
Related Issues:
Issue related from OData: OData/AspNetCoreOData#1027
Environment:
- EF Core Package:
Microsoft.EntityFrameworkCore.Cosmos
- EF Core Version:
6.0.21
- Database provider:
Microsoft.EntityFrameworkCore.Cosmos
- Target framework: .NET 6.0
- IDE: (e.g. Visual Studio 2022 17.6)
- Operating System: Windows 10
Steps to Reproduce:
using Microsoft.EntityFrameworkCore;
var options = new DbContextOptionsBuilder<CosmosDbContext>()
.UseCosmos("AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", "DummyDb")
.Options;
using var context = new CosmosDbContext(options);
var employeeList = context.Employee.Select(x => new EmployeeDTO { Id = x.id, Emails = x.User.Emails }).ToList();
public class CosmosDbContext : DbContext
{
public CosmosDbContext(DbContextOptions<CosmosDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<RootObject>().ToContainer("DummyContainer");
modelBuilder.Entity<RootObject>().HasNoDiscriminator();
}
public DbSet<RootObject> Employee { get; set; }
}
public class RootObject
{
public string id { get; set; }
public User User { get; set; }
public string _rid { get; set; }
public string _self { get; set; }
public string _etag { get; set; }
public string _attachments { get; set; }
public int _ts { get; set; }
}
public class User
{
public string Name { get; set; }
public ICollection<Emails> Emails { get; set; }
}
public class Emails
{
public string EmailAdress { get; set; }
public string Type { get; set; }
}
public class EmployeeDTO
{
public string Id { get; set; }
public ICollection<Emails> Emails { get; set; }
}
Cosmos document representation:
{
"id": "1",
"User": {
"Name": "John",
"Emails": [
{
"EmailAdress": "john@1.com",
"Type": "A"
},
{
"EmailAdress": "john@2.com",
"Type": "B"
}
]
},
"_rid": "jqYgAN5HGIMBAAAAAAAAAA==",
"_self": "dbs/jqYgAA==/colls/jqYgAN5HGIM=/docs/jqYgAN5HGIMBAAAAAAAAAA==/",
"_etag": "\"00000000-0000-0000-e557-09ab7b4901d9\"",
"_attachments": "attachments/",
"_ts": 1694508997
}
Expected Behavior:
I expected to successfully project a query result from Cosmos DB using Entity Framework Core without encountering any exceptions.
Actual Behavior:
While attempting to project a query result from Cosmos DB using Entity Framework Core, I encountered a System.NullReferenceException with the following stack trace:
System.NullReferenceException: Object reference not set to an instance of an object.
at lambda_method6(Closure , QueryContext , JObject )
at Microsoft.EntityFrameworkCore.Cosmos.Query.Internal.CosmosShapedQueryCompilingExpressionVisitor.QueryingEnumerable`1.Enumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Program.<Main>$(String[] args) in D:\Repos\CosmosBug\CosmosBug\Program.cs:line 8
Description:
When attempting to project a query result from Cosmos DB using Entity Framework Core, a System.NullReferenceException occurs with the stack trace pointing to lambda_method6.
Related Issues:
Issue related from OData: OData/AspNetCoreOData#1027
Environment:
Microsoft.EntityFrameworkCore.Cosmos6.0.21Microsoft.EntityFrameworkCore.CosmosSteps to Reproduce:
Cosmos document representation:
{ "id": "1", "User": { "Name": "John", "Emails": [ { "EmailAdress": "john@1.com", "Type": "A" }, { "EmailAdress": "john@2.com", "Type": "B" } ] }, "_rid": "jqYgAN5HGIMBAAAAAAAAAA==", "_self": "dbs/jqYgAA==/colls/jqYgAN5HGIM=/docs/jqYgAN5HGIMBAAAAAAAAAA==/", "_etag": "\"00000000-0000-0000-e557-09ab7b4901d9\"", "_attachments": "attachments/", "_ts": 1694508997 }Expected Behavior:
I expected to successfully project a query result from Cosmos DB using Entity Framework Core without encountering any exceptions.
Actual Behavior:
While attempting to project a query result from Cosmos DB using Entity Framework Core, I encountered a System.NullReferenceException with the following stack trace: