-
Notifications
You must be signed in to change notification settings - Fork 20
Truncate Tables
Pawel Gerr edited this page Mar 4, 2026
·
7 revisions
Adds support for TRUNCATE TABLE.
Looking for conditional bulk delete? EF Core natively supports deleting entities matching a condition via
ExecuteDeleteAsync().TRUNCATE TABLEremoves all rows from a table without conditions.
Add support for truncating tables.
If you are using Lazy Loading then disable the registration of temp tables for primites types
sqlOptions.AddBulkOperationSupport(configureTempTablesForPrimitiveTypes: false).
var services = new ServiceCollection()
.AddDbContext<DemoDbContext>(builder => builder
// SQL Server
.UseSqlServer("conn-string", sqlOptions =>
{
sqlOptions.AddBulkOperationSupport();
})
// PostgreSQL
//.UseNpgsql("conn-string", npgsqlOptions =>
// {
// npgsqlOptions.AddBulkOperationSupport();
// })
// SQLite
//.UseSqlite("conn-string", sqliteOptions =>
// {
// sqliteOptions.AddBulkOperationSupport();
// })await ctx.TruncateTableAsync<Customer>();PostgreSQL does not allow truncating tables that have foreign key references from other tables unless CASCADE is specified. Use the cascade parameter to also truncate dependent tables.
await ctx.TruncateTableAsync<Customer>(cascade: true);- Collection Parameters (temp-tables light)
- Window Functions Support (RowNumber, Sum, Average, Min, Max)
- Nested (virtual) Transactions
- Table Hints
- Queries across multiple databases
- Changing default schema at runtime
- If-Exists / If-Not-Exists checks in migrations
- Isolation of tests [DEPRECATED]