BulkDeleteByKey
This method deletes rows from the database using a list of primary keys in bulk. It is supported only for Oracle.
Use Case
Use this method to delete rows by primary key at high speed. It leverages the native bulk operation from ODP.NET via the OracleBulkCopy class.
Special Arguments
The bulkCopyTimeout, batchSize and pseudoTableType arguments are available for this operation.
batchSize overrides the number of rows sent to the server per batch. When not set, all items are sent at once.
pseudoTableType (via OracleBulkImportPseudoTableType) controls the kind of staging table used internally.
Every
pseudoTableTypevalue currently resolves toPhysicalat runtime — see Bulk Operations (Oracle) for details.
Usability
Pass the target table name and the list of primary keys to the operation.
using (var connection = new OracleConnection(connectionString))
{
var primaryKeys = connection.Query<Person>(p => p.IsActive == false).Select(p => p.Id);
var deletedRows = connection.BulkDeleteByKey("Person", primaryKeys);
}
It returns the number of rows deleted from the underlying table.
To specify a batch size:
using (var connection = new OracleConnection(connectionString))
{
var primaryKeys = connection.Query<Person>(p => p.IsActive == false).Select(p => p.Id);
var deletedRows = connection.BulkDeleteByKey("Person",
primaryKeys,
batchSize: 100);
}
Async Method
An equivalent BulkDeleteByKeyAsync method is also available.
using (var connection = new OracleConnection(connectionString))
{
var primaryKeys = connection.Query<Person>(p => p.IsActive == false).Select(p => p.Id);
var deletedRows = await connection.BulkDeleteByKeyAsync("Person", primaryKeys);
}