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: Selecting complex properties (object or collections mapped as "OwnsOne" and "OwnsMany") makes the query return the entire document #27440

Open
alexandrevribeiro opened this issue Feb 15, 2022 · 3 comments
Labels
area-cosmos area-query customer-reported punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-enhancement
Milestone

Comments

@alexandrevribeiro
Copy link

I'm using Microsoft.EntityFrameworkCore.Cosmos 6.0.1 to perform queries against a Cosmos DB, but the problem I'm facing is that whenever I include in the Select any property that is either an object or a collection/array (mapped as "OwnsOne" and "OwnsMany"), instead of querying only the selected properties, it actually selects the entire document for EF to be able to select the complex/collection property.

Code sample

Entities

public record Litigation
{
    public string Id { get; set; }
    public string IdentifierNumber { get; set; }
    public GenericEntity City { get; set; }
    public List<GenericEntity> Subjects { get; set; }
}

public record GenericEntity
{  
    public int Id { get; set; } 
    public string Name { get; set; }
}

Entity type configuration

Note: There might be probably a way to avoid using ToJsonProperty for each Cosmos camelCase property (maybe using serialized options), but I'm not worried about that for now.

public class LitigationTypeConfiguration : IEntityTypeConfiguration<Litigation>
{
    public void Configure(EntityTypeBuilder<Litigation> builder)
    {
        builder.ToContainer(CosmosConstants.LitigationContainerName)
           .HasPartitionKey(l => l.TenantId)
           .HasNoDiscriminator();

        builder.Property(t => t.Id).ToJsonProperty("id").IsRequired();
        builder.Property(t => t.IdentifierNumber).ToJsonProperty("identifierNumber");
        builder.OwnsOne(t => t.City,
            o =>
            {
                o.ToJsonProperty("city");
                o.Property(p => p.Id).ToJsonProperty("id");
                o.Property(p => p.Name).ToJsonProperty("name");
            });
        builder.OwnsMany(t => t.Subjects,
            o =>
            {
                o.ToJsonProperty("subjects");
                o.Property(p => p.Id).ToJsonProperty("id");
                o.Property(p => p.Name).ToJsonProperty("name");
            });
    }
}

Queries sample and Results

Query sample 1) Selecting only simple properties

Query sample:

var test = _context.Litigations
	.Where(l => l.Id == id)
	.WithPartitionKey("MyPartitionKey")
	.AsNoTracking()
	.Select(l => new
	{
		l.Id,
		l.IdentifierNumber,
	}).ToList();

Result:

Note that only the c["id"] and c["identifierNumber"] were selected, as expected.
image

Query sample 2) Selecting a complex object property (city)

Query sample:

var test = _context.Litigations
	.Where(l => l.Id == id)
	.WithPartitionKey("MyPartitionKey")
	.AsNoTracking()
	.Select(l => new
	{
		l.Id,
		l.IdentifierNumber,
		l.City // <-------------
	}).ToList();

Result:

In this case, the entire document is being selected (represented in the result as c), which shouldn't happen. If I copy and past this query on Azure Portal Cosmos Data Explorer, I can see it is indeed returning the entire document:
image

Query sample 3) Selecting a collection/array property (subjects)

Query sample:

var test = _context.Litigations
	.Where(l => l.Id == id)
	.WithPartitionKey("MyPartitionKey")
	.AsNoTracking()
	.Select(l => new
	{
		l.Id,
		l.IdentifierNumber,
		l.Subjects // <-------------
	}).ToList();

Result:

The same behavior happening when selecting a complex object is also happening with a collection property:
image

Provider and version information

EF Core version: 6.0.1
Database provider: Microsoft.EntityFrameworkCore.Cosmos 6.0.1
Target framework: .NET 6.0
Operating system: Windows
IDE: Visual Studio Professional 2022

@alexandrevribeiro alexandrevribeiro changed the title Cosmos: Selecting complex properties (object or collections mapped as "OwnsOne" and "OwnsMany") makes the query return the entire object Cosmos: Selecting complex properties (object or collections mapped as "OwnsOne" and "OwnsMany") makes the query return the entire document Feb 15, 2022
@AndriySvyryd AndriySvyryd added this to the 7.0.0 milestone Feb 15, 2022
@AndriySvyryd
Copy link
Member

Related to #16926

For tracking queries we need to select the owner (the document in this case), however this shouldn't be necessary for non-tracking queries

@ajcvickers ajcvickers added punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. and removed propose-punt labels Jul 7, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, Backlog Jul 7, 2022
@smitpatel smitpatel removed their assignment Sep 14, 2022
@samisq
Copy link

samisq commented Jan 26, 2023

+1 for this. We're observing the same issue with a rational provider (we're using Npgsql.EntityFrameworkCore.PostgreSQL v7.0).

@relfman-cmg
Copy link

I would also +1 this as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-cosmos area-query customer-reported punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-enhancement
Projects
None yet
Development

No branches or pull requests

6 participants