Class PostgresDataContext
Data context for PostgreSQL. Wraps the Npgsql driver for connections and parameters and
renders SQL through PostgresDialect.
public class PostgresDataContext : DataContext, IDataContext, IQueryExecutor, IQueryMaterializer, IQueryPlanner, IRowReaderFactory, IQueryCache, IContextEnvironment, IRawCommandExecutor, IAsyncDisposable, IDisposable, IConnectionManager, ITransactionManager
- Inheritance
-
PostgresDataContext
- Implements
- Inherited Members
- Extension Methods
Constructors
PostgresDataContext(DbConnection, DataContextBuilder)
Creates a PostgreSQL context over a caller-supplied connection, which the context does not dispose. The context is unversioned: every supported form is emitted.
public PostgresDataContext(DbConnection connection, DataContextBuilder optionsBuilder)
Parameters
connectionDbConnectionAn already-created
DbConnectionowned by the caller.optionsBuilderDataContextBuilderThe options collected from
DataContextBuilder.
PostgresDataContext(DbConnection, DataContextBuilder, Version?)
Creates a version-aware PostgreSQL context over a caller-supplied connection, which the context does not dispose.
public PostgresDataContext(DbConnection connection, DataContextBuilder optionsBuilder, Version? serverVersion)
Parameters
connectionDbConnectionAn already-created
DbConnectionowned by the caller.optionsBuilderDataContextBuilderThe options collected from
DataContextBuilder.serverVersionVersionThe PostgreSQL server version, or null for unset. Immutable per context type.
Exceptions
- InvalidOperationException
A different version was already registered for this context type.
PostgresDataContext(string, DataContextBuilder)
Creates a PostgreSQL context that owns a connection built lazily from
connectionString. The context is unversioned: every supported form is
emitted.
public PostgresDataContext(string connectionString, DataContextBuilder optionsBuilder)
Parameters
connectionStringstringThe connection string used to create the
NpgsqlConnection.optionsBuilderDataContextBuilderThe options collected from
DataContextBuilder.
PostgresDataContext(string, DataContextBuilder, Version?)
Creates a version-aware PostgreSQL context that owns a connection built lazily from
connectionString.
public PostgresDataContext(string connectionString, DataContextBuilder optionsBuilder, Version? serverVersion)
Parameters
connectionStringstringThe connection string used to create the
NpgsqlConnection.optionsBuilderDataContextBuilderThe options collected from
DataContextBuilder.serverVersionVersionThe PostgreSQL server version, or null for unset. Immutable per context type.
Exceptions
- InvalidOperationException
A different version was already registered for this context type.
Properties
Dialect
SQL dialect used to render queries. Supplied by the provider; SQL generation depends on this interface rather than on the context itself.
public override ISqlDialect Dialect { get; }
Property Value
SupportsRawRowColumns
True: PostgreSQL can surface an anonymous ROW(...) record column or a caller-registered
named composite as a single raw result-set column (#194).
protected override bool SupportsRawRowColumns { get; }
Property Value
TimeOnlyProjectionRefusal
PostgreSQL time is a true time of day and the driver surfaces it as
TimeSpan with microsecond precision, so the projection is supported.
protected override string? TimeOnlyProjectionRefusal { get; }
Property Value
Methods
BulkInsertRows(string, IReadOnlyList<string>, IReadOnlyList<IPropertyMetadata>, IEnumerable<object?[]>, int?, int?, Action<int>?, int, BulkCopyFlags)
Writes rows through COPY <table> (<cols>) FROM STDIN (FORMAT
BINARY) with NpgsqlBinaryImporter. A finite MaxBatchSize starts one importer per
bounded chunk (each completed on success and disposed on cancellation/error); an unbounded request
streams every row into one importer without buffering the set.
protected override int BulkInsertRows(string tableName, IReadOnlyList<string> columnNames, IReadOnlyList<IPropertyMetadata> columns, IEnumerable<object?[]> rows, int? commandTimeoutSeconds, int? maxBatchSize, Action<int>? progress, int notifyEvery, BulkCopyFlags bulkCopy)
Parameters
tableNamestringThe rendered target table reference (schema-qualified, quoted when configured).
columnNamesIReadOnlyList<string>The convention-resolved (unquoted) written column names, in row order.
columnsIReadOnlyList<IPropertyMetadata>The mapped columns, in row order.
rowsIEnumerable<object[]>The rows to write; each array matches
columnNamesby ordinal.commandTimeoutSecondsint?The
Timeoutbudget in whole seconds, or null for the provider default.maxBatchSizeint?The maximum rows per importer, or null to stream the whole set into one importer.
progressAction<int>Called with the cumulative written-row count after each completed chunk, or null.
notifyEveryintThe progress reporting interval in rows; the callback wrapper applies it.
bulkCopyBulkCopyFlagsThe bulk-copy flags requested by the caller;
COPYcannot express them.
Returns
- int
The number of rows written.
BulkInsertRowsAsync(string, IReadOnlyList<string>, IReadOnlyList<IPropertyMetadata>, IAsyncEnumerable<object?[]>, int?, int?, Action<int>?, int, BulkCopyFlags, CancellationToken)
Asynchronously writes rows through a binary COPY.
protected override Task<int> BulkInsertRowsAsync(string tableName, IReadOnlyList<string> columnNames, IReadOnlyList<IPropertyMetadata> columns, IAsyncEnumerable<object?[]> rows, int? commandTimeoutSeconds, int? maxBatchSize, Action<int>? progress, int notifyEvery, BulkCopyFlags bulkCopy, CancellationToken cancellationToken)
Parameters
tableNamestringThe rendered target table reference (schema-qualified, quoted when configured).
columnNamesIReadOnlyList<string>The convention-resolved (unquoted) written column names, in row order.
columnsIReadOnlyList<IPropertyMetadata>The mapped columns, in row order.
rowsIAsyncEnumerable<object[]>The rows to write; each array matches
columnNamesby ordinal.commandTimeoutSecondsint?The
Timeoutbudget in whole seconds, or null for the provider default.maxBatchSizeint?The maximum rows per importer, or null to stream the whole set into one importer.
progressAction<int>Called with the cumulative written-row count after each completed chunk, or null.
notifyEveryintThe progress reporting interval in rows; the callback wrapper applies it.
bulkCopyBulkCopyFlagsThe bulk-copy flags requested by the caller;
COPYcannot express them.cancellationTokenCancellationTokenCancels execution.
Returns
CreateDbConnection(string?)
Creates a new NpgsqlConnection for connectionString.
protected override DbConnection CreateDbConnection(string? connectionString)
Parameters
connectionStringstringThe PostgreSQL connection string.
Returns
- DbConnection
A new, unopened PostgreSQL connection.
CreateParam(string, object?)
Creates an NpgsqlParameter, mapping a null value to
DBNull.Value and binding JSON documents, elements and nodes as jsonb so that the
JSON operators accept them without an explicit cast.
public override DbParameter CreateParam(string name, object? value)
Parameters
namestringThe parameter name, without the provider prefix.
valueobjectThe parameter value, or null.
Returns
- DbParameter
A new PostgreSQL parameter.
CreateProcedureParameter(ProcedureParameter)
Creates an NpgsqlParameter for a descriptor. A TableParameterValue is
emulated: a scalar row type becomes a typed array (unnest(@p)/= ANY(@p)), an
entity row type becomes a jsonb document (jsonb_to_recordset(@p)). A
TypeName has no meaning on PostgreSQL and is rejected.
protected override DbParameter CreateProcedureParameter(ProcedureParameter parameter)
Parameters
parameterProcedureParameterThe parameter descriptor.
Returns
- DbParameter
A new PostgreSQL parameter configured from
parameter.
Exceptions
- ArgumentException
The descriptor carries a TypeName (SQL Server only), or a table parameter is not Input.
IsGenuineCompositeColumn(DbDataReader, int)
True only when reader is an Npgsql.NpgsqlDataReader that reports a
genuine PostgreSQL composite for ordinal (a caller-registered named or struct
composite). Any other reader, or a column whose catalog metadata is unknown (hstore/ltree
or an unresolved cold composite → Npgsql.PostgresTypes.UnknownBackendType), returns
false so classification never guesses from a data type name or an exception shape.
The metadata is read from the existing reader; no second command, connection or type reload is used.
protected override bool IsGenuineCompositeColumn(DbDataReader reader, int ordinal)
Parameters
readerDbDataReaderThe reader whose current result set is being classified.
ordinalintThe zero-based column ordinal.
Returns
MapColumnExpression(SelectExpression, Expression)
Materializes a projected Range<T> column from the driver's
NpgsqlRange<T> through the typed GetFieldValue accessor, converting it to the
provider-agnostic range. Every other column is mapped by the base implementation.
public override Expression MapColumnExpression(SelectExpression column, Expression param)
Parameters
columnSelectExpressionThe projected column being read.
paramExpressionThe data-reader expression the accessor is built from.
Returns
- Expression
An expression that reads the column value.