Skip to content

[release/10.0] Fix Contains handling for read-only collections - #39114

Merged
AndriySvyryd merged 4 commits into
release/10.0from
copilot/servicing-pr
Oct 2, 2026
Merged

AndriySvyryd merged 4 commits into
release/10.0from
copilot/servicing-pr

Conversation

Copilot AI commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #38671
Backports #37390

Description
EF Core only recognizes Contains implementations exposed through ICollection<T> when normalizing collection membership in queries. This misses IReadOnlySet<T> and custom IReadOnlyCollection<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 existing ICollection<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:

IReadOnlySet<long> ids = new HashSet<long> { 1, 2 };
var lines = db.APInvoiceLines
    .Where(line => ids.Contains(line.ApinvoiceId));

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 Contains query normalization. Quirk added.

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
Copilot AI requested a review from AndriySvyryd September 28, 2026 17:20
@AndriySvyryd
AndriySvyryd requested a lite review from Copilot September 28, 2026 19:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 High severity · 1 Medium severity · 1 Low severity

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> Contains calls.
  • Add the Issue37081 compatibility 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

@AndriySvyryd
AndriySvyryd marked this pull request as ready for review September 28, 2026 19:41
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner September 28, 2026 19:41

@artl93 artl93 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi-user reported, but non-regression. Non-obvious workaround. Approved.

@rbhanda rbhanda added this to the 10.0.15 milestone Oct 1, 2026
@AndriySvyryd
AndriySvyryd merged commit 6f00b41 into release/10.0 Oct 2, 2026
14 of 17 checks passed
@AndriySvyryd
AndriySvyryd deleted the copilot/servicing-pr branch October 2, 2026 01:44
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]>
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants