Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
roji opened this issue Sep 12, 2024 · 0 comments

Comments

@roji
Copy link
Member

roji commented Sep 12, 2024

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

_ = await context.Employee
    .Select(x => x.User.Emails)
    .SingleAsync();
Repro
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 = "[email protected]", Type = "A"},
            new() { EmailAdress = "[email protected]", Type = "B"}
        ]
    }
});
await context.SaveChangesAsync();

context.ChangeTracker.Clear();

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

public class CosmosDbContext : DbContext
{
    public DbSet<RootObject> 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<RootObject>().ToContainer("DummyContainer");
        modelBuilder.Entity<RootObject>().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> 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> Emails { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant