dotnet / dotnet/efcore

Cosmos: NullReferenceException when projecting out a collection of owned entity types

Open
#34,660 0 comments 1 reaction 0 assignees View on GitHub
area-cosmos area-query consider-for-next-release
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

This is another bug in a series of issues related to projecting out owned types in various ways (#31696, #34067):

```c#
_ = await context.Employee
.Select(x => x.User.Emails)
.SingleAsync();
```

Repro

```c#
await using var context = new CosmosDbContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

context.Employee.Add(new()
{
id = "1",
User = new()
{
Name = "John",
Emails = [
new() { EmailAdress = "john@1.com", Type = "A"},
new() { EmailAdress = "john@2.com", Type = "B"}
]
}
});
await context.SaveChangesAsync();

context.ChangeTracker.Clear();

_ = await context.Employee
.Select(x => x.User.Emails)
.SingleAsync();

public class CosmosDbContext : DbContext
{
public DbSet Employee { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseCosmos(
"https://192.168.64.6:8081",
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
"Test",
o => o.HttpClientFactory(() => new HttpClient(
new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
}))
.ConnectionMode(ConnectionMode.Gateway))
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity().ToContainer("DummyContainer");
modelBuilder.Entity().HasNoDiscriminator();
}
}

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 { get; set; }
}

public class Emails
{
public string EmailAdress { get; set; }
public string Type { get; set; }
}

public class EmployeeDTO
{
public string Id { get; set; }
public string Name { get; set; }
public ICollection Emails { get; set; }
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.