Skip to content

Disable filter applying for Truncate, Drop.#5419

Merged
MaceWindu merged 1 commit intomasterfrom
issue/5417-trancate-filters
Mar 12, 2026
Merged

Disable filter applying for Truncate, Drop.#5419
MaceWindu merged 1 commit intomasterfrom
issue/5417-trancate-filters

Conversation

@sdanyliv
Copy link
Member

Fixes #5417

Copilot AI review requested due to automatic review settings March 12, 2026 14:00
@sdanyliv sdanyliv requested review from Shane32 and jods4 as code owners March 12, 2026 14:00
@sdanyliv sdanyliv added this to the 6.3.0 milestone Mar 12, 2026
@sdanyliv
Copy link
Member Author

/azp run test-all

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #5417 where Truncate and Drop operations fail with an InvalidCastException when used on tables that have global query filters applied (e.g., soft-delete filters in EF Core). The root cause was that the builders directly cast the build sequence result to TableContext, but when query filters are present, the result is a SubQueryContext wrapper instead.

Changes:

  • Disable query filters when building sequences in TruncateBuilder and DropBuilder, and use SequenceHelper.GetTableContext() to safely extract the table context instead of a direct cast.
  • Refactor SequenceHelper.GetTableContext and GetTableOrCteContext to use the existing CreateRef helper method.
  • Add a regression test that verifies Truncate and Drop work correctly on tables with complex query filters.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
Source/LinqToDB/Internal/Linq/Builder/TruncateBuilder.cs Disable query filters during sequence building; use GetTableContext() instead of direct cast
Source/LinqToDB/Internal/Linq/Builder/DropBuilder.cs Same fix as TruncateBuilder: disable filters and use safe table context extraction
Source/LinqToDB/Internal/Linq/Builder/SequenceHelper.cs Minor refactoring to use CreateRef helper in GetTableContext and GetTableOrCteContext
Tests/Linq/Update/TruncateTableTests.cs Adds regression test with a query-filtered entity for Truncate and Drop operations

@linq2dbot
Copy link

Test baselines changed by this PR. Don't forget to merge/close baselines PR after this pr merged/closed.

MaceWindu added a commit to linq2db/linq2db.baselines that referenced this pull request Mar 12, 2026
* [Windows / SQL CE] baselines

* [Windows / SQL Server 2005] baselines

* [Windows / SQLite (both providers)] baselines

* [Windows / Access MDB (Jet/ODBC)] baselines

* [Windows / SQL Server 2012] baselines

* [Windows / Access ACE (OLEDB/ODBC) x86] baselines

* [Windows / SQL Server 2008] baselines

* [Linux / Firebird 2.5] baselines

* [Windows / SQL Server 2016] baselines

* [Linux / DB2 LUW 11.5] baselines

* [Linux / Firebird 3.0] baselines

* [Linux / Informix 14.10] baselines

* [Linux / Firebird 4.0] baselines

* [Linux / Firebird 5.0] baselines

* [Linux / MariaDB 11] baselines

* [Windows / SQL Server 2014] baselines

* [Linux / ClickHouse Driver] baselines

* [Windows / SQL Server 2017] baselines

* [Windows / SQL Server 2019] baselines

* [Linux / ClickHouse MySql] baselines

* [Linux / MySQL 5.7 (both providers)] baselines

* [Linux / MySQL 9 (both providers)] baselines

* [Linux / ClickHouse Octonica] baselines

* [Linux / Oracle 11g XE] baselines

* [Linux / PostgreSQL 13] baselines

* [Linux / PostgreSQL 14] baselines

* [Windows / SQL Server 2022] baselines

* [Linux / Oracle 12c] baselines

* [Linux / PostgreSQL 15] baselines

* [Linux / Oracle 18c] baselines

* [Linux / Oracle 23c] baselines

* [Linux / Oracle 19c] baselines

* [Linux / Oracle 21c] baselines

* [Linux / PostgreSQL 16] baselines

* [Linux / PostgreSQL 18] baselines

* [Linux / PostgreSQL 17] baselines

* [Linux / SQLite (both providers)] baselines

* [Linux / Sybase ASE 16] baselines

* [Linux / SQL Server 2025] baselines

* [Linux / SAP HANA 2] baselines

* [Linux / SQL Server EXTRAS] baselines

---------

Co-authored-by: Azure Pipelines Bot <azp@linq2db.com>
Co-authored-by: MaceWindu <MaceWindu@users.noreply.github.com>
@MaceWindu MaceWindu merged commit f3abde5 into master Mar 12, 2026
58 checks passed
@MaceWindu MaceWindu deleted the issue/5417-trancate-filters branch March 12, 2026 21:32
@MaceWindu MaceWindu modified the milestones: 6.3.0, 6.2.1 Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Truncate fails for EF Core DbSet<T> with global query filter

5 participants