Link Search Menu Expand Document

PostgreSqlBulkInsertMapItem


This class extends BulkInsertMapItem with an optional, explicit NpgsqlDbType or PostgreSQL data type name to bind with for the mapped column. When neither is provided, the type is resolved from the .NET CLR type via the ClientTypeToNpgsqlDbTypeResolver.

Used by the PostgreSQL BulkInsert, BulkMerge and BulkUpdate operations.

This class replaces NpgsqlBulkInsertMapItem, which is now deprecated. Existing code using NpgsqlBulkInsertMapItem continues to work, as it is now a subclass of PostgreSqlBulkInsertMapItem kept only for backward compatibility.

Create a new Instance

var mapItem = new PostgreSqlBulkInsertMapItem("SourceId", "DestinationId");

Or with an explicit .NET CLR Type (resolved to an NpgsqlDbType):

var mapItem = new PostgreSqlBulkInsertMapItem("SourceAge", "DestinationAge", typeof(int));

Or with an explicit NpgsqlDbType:

var mapItem = new PostgreSqlBulkInsertMapItem("SourceName", "DestinationName", NpgsqlDbType.Varchar);

Or with an explicit PostgreSQL data type name (e.g. a native enum type), which takes precedence over NpgsqlDbType when both are relevant:

var mapItem = new PostgreSqlBulkInsertMapItem("SourceHand", "DestinationHand", "hand");

Usage for BulkOperations

var mappings = new []
{
    new PostgreSqlBulkInsertMapItem("FirstName", "FName"),
    new PostgreSqlBulkInsertMapItem("LastName", "LName"),
    new PostgreSqlBulkInsertMapItem("Age", "Age", NpgsqlDbType.Integer),
    new PostgreSqlBulkInsertMapItem("Hand", "Hand", "hand")
};

using (var connection = new NpgsqlConnection(connectionString))
{
    var people = GetPeople(100000);
    connection.BulkInsert(people, mappings: mappings);
}

The same approach applies to BulkMerge and BulkUpdate. The mappings argument is optional — omitting it causes the library to auto-map columns by name (case-insensitive).