Repository navigation
fix: keep RangeSet.Contains out of EF Core's rewrite of Contains on a read-only collection - #310
Merged
CaffeinatedCoder merged 2 commits intoOct 19, 2026
Conversation
… 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]>
CaffeinatedCoder
enabled auto-merge
October 19, 2026 12:16
CaffeinatedCoder
deleted the
fix/288-rangeset-contains-ef-core-patches
branch
October 19, 2026 12:22
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.
Closes #288
#288 is fixed only on
release/10.x(maincarries the 11-line twin #287 through #292), soCloseswill not act: close it by hand after the merge, with a link here.Problem
The EF Core 10 patch that carries dotnet/efcore#37081 (dotnet/efcore#39114, milestone 10.0.15) makes
QueryableMethodNormalizingExpressionVisitorrewrite every one-argument method namedContainson anIReadOnlyCollection<T>intoQueryable.Containsover the collection's element type, before any translator runs.RangeSet<TRange, T>is anIReadOnlyList<TRange>whoseContainsoverloads take aT, anIRange<T>or aRangeSet, so on that patch everyRangeSet.Containsin a query throwsArgumentExceptioninside EF Core. EF Core 10.0.0 through 10.0.13 rewrite onlyICollection<T>, which is why the floor this repository's CI pins never sees it (#297); Npgsql EF 10.0.1–10.0.3 require EF Core ≥ 10.0.4, so a consumer reaches 10.0.15 with a plain restore or a Dependabot bump. Reported upstream as dotnet/efcore#39329.Change
src/CodoMetis.ValueRanges.EFCore.PostgreSQL/Query/RangeSetContains.cs(new, internal): the threeRangeSet.Containsoverloads as static methodsValue,RangeandSet, each calling the instance method it stands for, plusEquivalent(MethodInfo), which picks the static form by the operand's type.ValueRangesQueryExpressionInterceptor.cs: a fifth rewrite,RangeSetContainsRewriter, turnsset.Contains(x)into the matching static call. Its declaring type is not a collection, so EF Core's rewrite leaves it alone on every patch.ValueRangesMethodCallTranslator.cs: translates the static form to@>exactly as the instance call (TranslateRangeSetCallnow takes the method name, and the static form passes the set argument as the instance).main(231f23a), ported by hand: build: retarget main to .NET 11 for 11.0.0-preview.1 #292 is one commit holding the whole .NET 11 retarget, so there is no commit tocherry-pick -x. Public API unchanged.test/oracles/seeds/compareto-null-*.patch: the two seeds that diff the interceptor's chain and remarks are refreshed to the new context (as build: retarget main to .NET 11 for 11.0.0-preview.1 #292 did onmain);OracleSeedTestsrequires every seed to apply.## [10.1.0]in the root and the EF Core package changelog, naming the patch and the stopgapAppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue37081", true)with its cost (EF Core's own fix forIReadOnlySetparameters is lost, they inline their values again); the EF Core NodaTime changelog's intro line notes the satellite's sets get the fix through the base package.No docs change: nothing a consumer writes changes.
Tests
No published EF Core 10 patch carries the change yet, so the proof is the query tree, not the SQL.
test/CodoMetis.ValueRanges.EFCore.PostgreSQL.Tests/InterceptedQuery.cs(linked into the NodaTime translation tests) runs the context's interceptor chain through the same logger callQueryCompilationContextmakes and lists the calls EF Core's normalization rewrites: a one-argument instanceContainswhose declaring type is, or implements,ICollection<>orIReadOnlyCollection<>.RangeSetContainsShapeTests(5 + 1): forContains(T),Contains(IRange<T>)andContains(RangeSet)on the date set, a captured set as receiver, and the integer set — no such call survives, exactly oneRangeSetContains.<overload>stands in its place, and the SQL is still@>with the whole operand asserted (b."BlockedDays" @> DATE '2024-06-15',@> b."Period",@> @other,@other @> b."Period",b."SeatBlocks" @> 5).StaticForms_AnswerAsTheInstanceMethodschecksEquivalentpicksValue/Range/Setby operand type, answers as the instance methods, and returns null forOverlapsandList<int>.Contains.NodaRangeSetContainsShapeTests(8): the same forRangeSet<LocalDateRange, LocalDate>,RangeSet<InstantRange, Instant>(@> TIMESTAMPTZ '2024-06-15T12:00:00Z') and the month-alignedRangeSet<YearMonthRange, YearMonth>(@> DATE '2024-06-01').RangeSet.Containstranslation and integration test is unchanged.src/on the floor)The float used a scratch
test/Directory.Build.targets(not committed) addingMicrosoft.EntityFrameworkCore*andNpgsql.EntityFrameworkCore.PostgreSQL(.NodaTime)references to the test hosts (-p:EfFloat=10.0.13 -p:NpgsqlEfFloat=10.0.3;project.assets.jsonconfirmedMicrosoft.EntityFrameworkCore/10.0.13,Npgsql.EntityFrameworkCore.PostgreSQL/10.0.3).Daily servicing build (#297): the dnceng feed
https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet10/nuget/v3/index.jsonanswers (HTTP 200) but itsMicrosoft.EntityFrameworkCorelisting ends at10.0.0-rtm.25513.102— it holds no10.0.xservicing build, anddotnet10-transportdoes not list the package at all. So the run against a build carrying dotnet/efcore#39114 could not be made from here; the first published 10.0.15 is the moment the newest-patch leg of #297 proves this on CI.Revert proof (
verify-the-guard): with the one line that callsRangeSetContainsRewriterstripped from the interceptor's chain and everything else kept, the tree tests fail and every@>SQL test passes — on the floor and on the float alike, which is exactly why the defect was invisible to the SQL tests:The failure each tree test gives with the rewrite stripped:
🤖 Generated with Claude Code