Repository navigation
EF-357: Fix ArgumentException on bare embedded-collection Count projections - #337
Closed
ajcvickers wants to merge 1 commit into
Closed
ajcvickers wants to merge 1 commit into
ajcvickers wants to merge 1 commit into
Conversation
…ctions db.Set<T>().Select(e => e.OwnedCollection.Count) threw ArgumentException in every case (bare and arithmetic-wrapped alike), because EF Core lowers a collection-navigation Count to Queryable.Count(Queryable.AsQueryable(source)) even for an in-document embedded List<T>, and MongoProjectionBindingExpressionVisitor had no case for that shape - it fell into the generic fallback, which tried to rebuild the Queryable.Count call with the already-materialized List<T>, which isn't an IQueryable<T>. Add TryBindEmbeddedCollectionNavigationCount to recognize the shape and rewrite it as a null-safe Enumerable.Count/LongCount over the navigation's CollectionShaperExpression (null-safe because a missing/explicitly-null stored array materializes to null rather than an empty collection).
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a query translation/binding gap in the MongoDB EF Core provider where projecting OwnedCollection.Count (or LongCount) over an embedded owned collection could throw ArgumentException due to EF Core lowering the expression into a Queryable.Count(Queryable.AsQueryable(source)) shape that doesn’t match the provider’s fallback binding logic.
Changes:
- Add a dedicated binding path in
MongoProjectionBindingExpressionVisitorto detect embedded-navigationCount/LongCountlowered asQueryable.*(Queryable.AsQueryable(...))and rewrite to a null-safeEnumerable.Count/LongCountover theCollectionShaperExpression. - Add functional tests to cover bare
Count/LongCountprojections and arithmetic-wrappedCountover an owned embedded collection, including null/empty cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/MongoDB.EntityFrameworkCore.FunctionalTests/Query/OwnedEntityTests.cs | Adds regression tests for Count/LongCount projections over embedded owned collections (bare and wrapped). |
| src/MongoDB.EntityFrameworkCore/Query/Visitors/MongoProjectionBindingExpressionVisitor.cs | Adds embedded-collection Count/LongCount binding logic and null-safe helpers to avoid Queryable.Count rebuild failures. |
Suppressed comments (1)
src/MongoDB.EntityFrameworkCore/Query/Visitors/MongoProjectionBindingExpressionVisitor.cs:763
- NullSafeLongCount is intended to tolerate a null embedded collection (mirroring NullSafeCount), but its parameter is declared non-nullable IEnumerable even though the implementation treats it as nullable. Make the parameter nullable so the signature matches behavior and intent under enable.
private static long NullSafeLongCount<T>(IEnumerable<T> source)
=> source?.LongCount() ?? 0;
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Collaborator
Author
|
Superseded by #339 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
db.Set().Select(e => e.OwnedCollection.Count) threw ArgumentException in every case (bare and arithmetic-wrapped alike), because EF Core lowers a collection-navigation Count to Queryable.Count(Queryable.AsQueryable(source)) even for an in-document embedded List, and
MongoProjectionBindingExpressionVisitor had no case for that shape - it fell into the generic fallback, which tried to rebuild the Queryable.Count call with the already-materialized List, which isn't an IQueryable.
Add TryBindEmbeddedCollectionNavigationCount to recognize the shape and rewrite it as a null-safe Enumerable.Count/LongCount over the navigation's CollectionShaperExpression (null-safe because a missing/explicitly-null stored array materializes to null rather than an empty collection).