You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cosmos: Projecting out nested documents retrieves the entire document #34067
Since PersonAddress owns further entity types (OwnedCountry), the expression inside the Select() contains IncludeExpression; this leads CosmosProjectionBindingExpressionVisitor into a specific path where we end up projecting the entire document. Compare how this works in relational - CosmosProjectionBindingExpressionVisitor is one place where Cosmos is still quite behind.
changed the title [-]Cosmos: Projecting out owned entities retrieves the entire document[/-][+]Cosmos: Projecting out owned entities which own additional entity types retrieves the entire document[/+]on Jun 22, 2024
We indeed discussed similar cases back in EF 9 when redoing significant parts of the Cosmos query pipeline. Back then, of course, the discussion was restricted to projecting pure scalars, since the pipeline didn't (and still doesn't) support projecting out nested documents without the entire document.
So the cases you're outlining above are new, and I admit they're not great... Here are some thoughts:
For the first case, which IIUC is a problem only for AsNoTrackingWithIdentityResolution with owned entities; I generally think this isn't important enough. The thing to concentrate on going forward is IMHO complex types, where none of this can occur (complex types are always projected out as non-tracking and without any sort of identity). IMHO this also highlights the general weirdness of using the owned entity concept to map something that does not actually have an identity.
For entity vs. anonymous type projection, if we squint a bit, I think the behavior actually makes sense. Anonymous type projection means there's a new in the projection, so the result indeed should never be undefined (at least I hope my understanding of the current behavior is correct) - it only be missing certain properties inside (since they're undefined). Whereas projecting structural types via properties is inherently different, and if the projected structural type is truly absent from the document, then the general "filter out absent/undefined" makes sense (to me), even if I can see how it could be surprising. But I'm convinced it's any more surprising than the existing filtering-out behavior of scalars which we already have today.
I'm unaware of the behavioral difference you're describing between projecting an anonymous type with one property vs. two; that indeed sounds like a separate/unrelated bug. Can you open a separate issue with a repro?
The main thing I'm trying to do here, is to stick to the database semantics/behavior as much as possible, and to avoid EF imposing an external behavior which we believe is better. In other words, I'm not a fan of the Cosmos behavior around filtering out undefined (though I understand where it comes from - Cosmos returns JSON and there's no concept of undefined there); but that's the Cosmos behavior that we're surfacing. Another way of looking at this is that someone using Cosmos without EF will run into exactly the same behavior - it's just how Cosmos works - and I don't think it should generally be our job to change or "repair" database behavior, just to expose it.
But the above are just my own views - what are your thoughts on the above? Also, this is definitely something to be discussed with @AndriySvyryd.
@roji That makes a lot of sense, thanks for explaining.
The main thing I'm trying to do here, is to stick to the database semantics/behavior as much as possible, and to avoid EF imposing an external behavior which we believe is better. In other words, I'm not a fan of the Cosmos behavior around filtering out undefined (though I understand where it comes from - Cosmos returns JSON and there's no concept of undefined there); but that's the Cosmos behavior that we're surfacing. Another way of looking at this is that someone using Cosmos without EF will run into exactly the same behavior - it's just how Cosmos works - and I don't think it should generally be our job to change or "repair" database behavior, just to expose it.
Being the devils advocate, SELECT VALUE is opt-in behavior in the cosmos query langue. On the other hand, I did some testing and the (.NET) Cosmos SDK ItemLinqQueryable does also translate these cases to SELECT VALUE, and causes the same behavior for entity vs. anonymous type projection. So someone using the SDK would indeed run into the same and I think the new is never undefined argument makes sense there.
For the first case, which IIUC is a problem only for AsNoTrackingWithIdentityResolution with owned entities; I generally think this isn't important enough. The thing to concentrate on going forward is IMHO complex types, where none of this can occur (complex types are always projected out as non-tracking and without any sort of identity). IMHO this also highlights the general weirdness of using the owned entity concept to map something that does not actually have an identity.
I had similar thoughts about this case as well, but was also wondering whether there might be other cases now (or very likely in the future?), where EF might need to project out 2 values where the user query is only selecting one, opening up the same question again.
Assuming we are keeping the SELECT VALUE behaviour, my main question on AsNoTrackingWithIdentityResolution is how I should move forward in the context of this issue. Are you implying we will just accept different filtering behavior for AsNoTrackingWithIdentityResolution for now? Maybe also good to note that relational currently always projects out the primary key of the owner entity, but I think I could make it so cosmos only projects it out when AsNoTrackingWithIdentityResolution is used. Or are you suggesting dropping support for AsNoTrackingWithIdentityResolution (edit: for owned entities) in cosmos, keeping in mind we will eventually drop support anyway?
For context: in #38298 (comment) , it was discussed that SELECT VALUE will remain for now, but is subject for removal in the future.
@AndriySvyryd In this issue, I am still uncertain how to proceed with AsNoTrackingWithIdentityResolution considering it would cause inconsistent filtering behaviour.
See:
Assuming we are keeping the SELECT VALUE behaviour, my main question on AsNoTrackingWithIdentityResolution is how I should move forward in the context of this issue. Are you implying we will just accept different filtering behavior for AsNoTrackingWithIdentityResolution for now? Maybe also good to note that relational currently always projects out the primary key of the owner entity, but I think I could make it so cosmos only projects it out when AsNoTrackingWithIdentityResolution is used. Or are you suggesting dropping support for AsNoTrackingWithIdentityResolution (edit: for owned entities) in cosmos, keeping in mind we will eventually drop support anyway?
To get us to a place of some consistency I propose to apply this rule of thumb: "Any naked projection will be translated as SELECT VALUE and any top-level instantiation (DTO/anon/entity/complex) in the projection won't use SELECT VALUE".
AsNoTrackingWithIdentityResolution on a query that doesn't already project out the entity or its key should throw.
Activity
[-]Cosmos: Projecting out owned entities retrieves the entire document[/-][+]Cosmos: Projecting out owned entities which own additional entity types retrieves the entire document[/+]Handle projections of nested owned entity in Cosmos
Handle projections of nested owned entity in Cosmos
17 remaining items
roji commentedon May 19, 2026
@JoasE thanks for this analysis.
We indeed discussed similar cases back in EF 9 when redoing significant parts of the Cosmos query pipeline. Back then, of course, the discussion was restricted to projecting pure scalars, since the pipeline didn't (and still doesn't) support projecting out nested documents without the entire document.
So the cases you're outlining above are new, and I admit they're not great... Here are some thoughts:
newin the projection, so the result indeed should never be undefined (at least I hope my understanding of the current behavior is correct) - it only be missing certain properties inside (since they're undefined). Whereas projecting structural types via properties is inherently different, and if the projected structural type is truly absent from the document, then the general "filter out absent/undefined" makes sense (to me), even if I can see how it could be surprising. But I'm convinced it's any more surprising than the existing filtering-out behavior of scalars which we already have today.The main thing I'm trying to do here, is to stick to the database semantics/behavior as much as possible, and to avoid EF imposing an external behavior which we believe is better. In other words, I'm not a fan of the Cosmos behavior around filtering out undefined (though I understand where it comes from - Cosmos returns JSON and there's no concept of undefined there); but that's the Cosmos behavior that we're surfacing. Another way of looking at this is that someone using Cosmos without EF will run into exactly the same behavior - it's just how Cosmos works - and I don't think it should generally be our job to change or "repair" database behavior, just to expose it.
But the above are just my own views - what are your thoughts on the above? Also, this is definitely something to be discussed with @AndriySvyryd.
JoasE commentedon May 19, 2026
@roji That makes a lot of sense, thanks for explaining.
Being the devils advocate,
SELECT VALUEis opt-in behavior in the cosmos query langue. On the other hand, I did some testing and the (.NET) Cosmos SDKItemLinqQueryabledoes also translate these cases toSELECT VALUE, and causes the same behavior for entity vs. anonymous type projection. So someone using the SDK would indeed run into the same and I think thenewis never undefined argument makes sense there.I had similar thoughts about this case as well, but was also wondering whether there might be other cases now (or very likely in the future?), where EF might need to project out 2 values where the user query is only selecting one, opening up the same question again.
Assuming we are keeping the
SELECT VALUEbehaviour, my main question on AsNoTrackingWithIdentityResolution is how I should move forward in the context of this issue. Are you implying we will just accept different filtering behavior for AsNoTrackingWithIdentityResolution for now? Maybe also good to note that relational currently always projects out the primary key of the owner entity, but I think I could make it so cosmos only projects it out when AsNoTrackingWithIdentityResolution is used. Or are you suggesting dropping support for AsNoTrackingWithIdentityResolution (edit: for owned entities) in cosmos, keeping in mind we will eventually drop support anyway?JoasE commentedon Jun 16, 2026
For context: in #38298 (comment) , it was discussed that
SELECT VALUEwill remain for now, but is subject for removal in the future.@AndriySvyryd In this issue, I am still uncertain how to proceed with
AsNoTrackingWithIdentityResolutionconsidering it would cause inconsistent filtering behaviour.See:
AndriySvyryd commentedon Jun 17, 2026
To get us to a place of some consistency I propose to apply this rule of thumb: "Any naked projection will be translated as
SELECT VALUEand any top-level instantiation (DTO/anon/entity/complex) in the projection won't useSELECT VALUE".AsNoTrackingWithIdentityResolutionon a query that doesn't already project out the entity or its key should throw.AndriySvyryd commentedon Jun 19, 2026
@JoasE Just FYI - July 27 is the last day we can merge a big change like this one for v11
Cosmos: Modernize materializer & subdocument projectionbinding (#38550)