Repository navigation
[release/10.0] Fix Contains handling for read-only collections - #39114
Merged
Merged
Conversation
Co-authored-by: AndriySvyryd <[email protected]>
Co-authored-by: AndriySvyryd <[email protected]>
Co-authored-by: AndriySvyryd <[email protected]>
Co-authored-by: AndriySvyryd <[email protected]>
Copilot
AI
changed the title
[release/10.0] Fix handling of Contains on IReadOnlySet and custom IReadOnlyCollection
[release/10.0] Fix Contains handling for read-only collections
Sep 28, 2026
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The normalization can drop custom Contains comparer arguments and misses valid non-generic read-only collection implementations.
Review effort: Lite
Findings: 1
Open (3)
What changed in this PR
Fixes Contains translation for read-only collections while adding compatibility behavior and provider regression coverage.
Changes:
- Normalize
IReadOnlyCollection<T>/IReadOnlySet<T>Containscalls. - Add the
Issue37081compatibility switch. - Add SQL Server, SQLite, and Cosmos regression baselines.
| File | Description |
|---|---|
test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQuerySqlServerTest.cs |
SQL Server baselines |
test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQuerySqlServerJsonTypeTest.cs |
JSON-type SQL Server baselines |
test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQuerySqlServer160Test.cs |
SQL Server 160 baselines |
test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQueryOldSqlServerTest.cs |
Legacy SQL Server baselines |
test/EFCore.Sqlite.FunctionalTests/Query/PrimitiveCollectionsQuerySqliteTest.cs |
SQLite baselines |
test/EFCore.Specification.Tests/Query/PrimitiveCollectionsQueryTestBase.cs |
Regression tests and custom collection |
test/EFCore.Cosmos.FunctionalTests/Query/PrimitiveCollectionsQueryCosmosTest.cs |
Cosmos baselines |
test/EFCore.Cosmos.FunctionalTests/Query/NorthwindAggregateOperatorsQueryCosmosTest.cs |
Cosmos read-only set baseline |
src/EFCore/Query/Internal/QueryableMethodNormalizingExpressionVisitor.cs |
Read-only collection normalization and compatibility switch |
artl93
approved these changes
Sep 29, 2026
artl93
left a comment
Member
There was a problem hiding this comment.
Multi-user reported, but non-regression. Non-obvious workaround. Approved.
cincuranet
approved these changes
Oct 1, 2026
This was referenced Oct 2, 2026
wtgodbe
added a commit
that referenced
this pull request
Oct 7, 2026
* [release/10.0]: Bump Grpc.AspNetCore from 2.83.0 to 2.84.0 (#39141) --- updated-dependencies: - dependency-name: Grpc.AspNetCore dependency-version: 2.84.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * [release/10.0]: Bump OpenTelemetry.Exporter.InMemory from 1.19.0 to 1.19.1 (#39143) --- updated-dependencies: - dependency-name: OpenTelemetry.Exporter.InMemory dependency-version: 1.19.1 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * [release/10.0]: Bump Microsoft.Azure.Cosmos from 3.63.1 to 3.63.2 (#39142) * [release/10.0] Fix Contains handling for read-only collections (#39114) Fixes #38671 Co-authored-by: AndriySvyryd <[email protected]> * [release/10.0] Update stale nested complex collection entries during Update on a shortened collection (#39092) Fixes #39073 Co-authored-by: AndriySvyryd <[email protected]> * [release/11.0] Update xUnit v3 to 4.0.1 (#39144) Co-authored-by: AndriySvyryd <[email protected]> * Fix SQL Server JSON index test on release/11.0 (#39176) - Remove unsupported uniqueness from the JSON index test - Match the existing test on main Co-authored-by: Will Godbe <[email protected]> Co-authored-by: Copilot <[email protected]> --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]> Co-authored-by: AndriySvyryd <[email protected]> Co-authored-by: Andriy Svyryd <[email protected]> Co-authored-by: Andriy Svyryd <[email protected]> Co-authored-by: William Godbe <[email protected]> Co-authored-by: Will Godbe <[email protected]> Co-authored-by: Copilot <[email protected]>
This was referenced Oct 19, 2026
CaffeinatedCoder
added a commit
to CaffeinatedCoder/CodoMetis.ValueRanges
that referenced
this pull request
Oct 19, 2026
… read-only collection The EF Core 10 patch that carries dotnet/efcore#37081 (dotnet/efcore#39114, milestone 10.0.15) makes QueryableMethodNormalizingExpressionVisitor rewrite every one-argument method named Contains on an IReadOnlyCollection<T> into Queryable.Contains over the collection's element type, before any translator runs. RangeSet<TRange, T> is an IReadOnlyList<TRange> whose Contains overloads take a T, an IRange<T> or a RangeSet, so on that patch every RangeSet.Contains in a query threw ArgumentException inside EF Core. EF Core 10.0.0 through 10.0.13 rewrite only ICollection<T>, which is why the floor this repository's CI pins never saw it (#297); Npgsql EF 10.0.1-10.0.3 require EF Core >= 10.0.4, so a consumer reaches the patch with a plain restore. Reported upstream as dotnet/efcore#39329. The query interceptor now turns the three instance overloads into static methods on RangeSetContains, a declaring type that is not a collection and that EF Core's rewrite therefore leaves alone, and ValueRangesMethodCallTranslator translates that form to @> exactly as the instance call. Each static method calls the instance method it stands for, so a client-evaluated call answers as the set does. Public API unchanged. This is the fix PR #292 made on main for the 11 line (#287), ported by hand: #292 is one commit holding the whole .NET 11 retarget, so there is no commit to cherry-pick. No published EF Core 10 patch carries the change yet (the dnceng dotnet10 feed answers but lists no 10.0.x servicing build), so the proof is the query tree rather than the SQL. RangeSetContainsShapeTests and NodaRangeSetContainsShapeTests read the query as the interceptor hands it over (InterceptedQuery, through the logger call QueryCompilationContext makes) and assert that no instance Contains whose declaring type implements IReadOnlyCollection<> survives, that exactly one RangeSetContains call stands in its place, and that the SQL is still @>, for the three overloads on the BCL date and integer sets and on the NodaTime LocalDate, Instant and YearMonth sets. With the rewrite stripped from the interceptor the 5 and 8 tree tests fail and every @> SQL test still passes, on EF Core 10.0.0 and with the test hosts floated to EF Core 10.0.13 / Npgsql EF 10.0.3, which is exactly why the SQL tests could not see the defect. With it, 295/295 and 91/91 translation tests pass on both, and 280 + 4 integration tests pass on PostgreSQL 16. The two CompareTo seeds under test/oracles/seeds that diff the interceptor's chain and remarks are refreshed to the new context, as #292 did on main. The changelog line names the patch and the stopgap AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue37081", true), whose cost is EF Core's own fix for IReadOnlySet parameters. Issue #288, fixed on release/10.x only (main has #287 through #292); closed by hand after the merge, since a closing keyword acts only on main. Found-By: OT Defect-Class: SQL Co-Authored-By: Claude Fable 5.1 <[email protected]>
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.



Fixes #38671
Backports #37390
Description
EF Core only recognizes
Containsimplementations exposed throughICollection<T>when normalizing collection membership in queries. This missesIReadOnlySet<T>and customIReadOnlyCollection<T>implementations, causing some queries to fail translation and others to inline values instead of using parameterized collection SQL. This backport recognizes the read-only collection interface while preserving existingICollection<T>behavior.Customer impact
Queries using a read-only set may generate SQL with literal values instead of parameterized collection SQL, while a custom read-only collection may fail translation entirely. For example:
A workaround is to copy the values to an array or list before using them in the query.
How found
User-reported on EF Core 9.x and 10.x by 4 distinct reporters.
Regression
No.
Testing
Tests added.
Risk
Low. The change only affects
Containsquery normalization. Quirk added.