mvdmio.Database.PgSQL 0.26.0

dotnet add package mvdmio.Database.PgSQL --version 0.26.0
                    
NuGet\Install-Package mvdmio.Database.PgSQL -Version 0.26.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="mvdmio.Database.PgSQL" Version="0.26.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="mvdmio.Database.PgSQL" Version="0.26.0" />
                    
Directory.Packages.props
<PackageReference Include="mvdmio.Database.PgSQL" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add mvdmio.Database.PgSQL --version 0.26.0
                    
#r "nuget: mvdmio.Database.PgSQL, 0.26.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package mvdmio.Database.PgSQL@0.26.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=mvdmio.Database.PgSQL&version=0.26.0
                    
Install as a Cake Addin
#tool nuget:?package=mvdmio.Database.PgSQL&version=0.26.0
                    
Install as a Cake Tool

mvdmio.Database.PgSQL

PostgreSQL access for .NET applications.

The package combines Dapper and Npgsql with a higher-level API for common PostgreSQL workflows.

Installation

dotnet add package mvdmio.Database.PgSQL

Targets net8.0, net9.0, and net10.0.

What You Can Do With It

  • Run SQL queries and commands through db.Dapper
  • Execute work inside transactions
  • Bulk insert or upsert rows
  • Check schema and table existence
  • Export the current database schema
  • Limit schema export to selected PostgreSQL schemas when using the companion CLI configuration
  • Preserve identity columns and stored generated columns during schema export
  • Run migrations from application code
  • Generate repositories from annotated table models
  • Auto-embed Schemas/**/*.sql files for direct and transitive project references

Quick Start

using mvdmio.Database.PgSQL;

await using var db = new DatabaseConnection(
   "Host=localhost;Database=mydb;Username=postgres;Password=secret"
);

var users = await db.Dapper.QueryAsync<User>(
   "SELECT * FROM users WHERE active = :active",
   new Dictionary<string, object?> { ["active"] = true }
);

await db.Dapper.ExecuteAsync(
   "INSERT INTO users (name, email) VALUES (:name, :email)",
   new Dictionary<string, object?>
   {
      ["name"] = "Alice",
      ["email"] = "alice@example.com"
   }
);

Common Usage

Transactions

await db.InTransactionAsync(async () =>
{
   await db.Dapper.ExecuteAsync(
      "INSERT INTO orders (customer_id, total) VALUES (:customerId, :total)",
      new Dictionary<string, object?>
      {
         ["customerId"] = 42,
         ["total"] = 99.95m
      }
   );
});

Bulk Operations

var mapping = new Dictionary<string, Func<Product, DbValue>>
{
   ["sku"] = x => x.Sku,
   ["name"] = x => x.Name,
   ["price"] = x => x.Price
};

await db.Bulk.CopyAsync("products", products, mapping);

For streaming COPY sessions, prefer await using so failed writes still dispose the importer and release the connection:

await using var session = await db.Bulk.BeginCopyAsync<Product>("products", mapping);

foreach (var product in products)
   await session.WriteAsync(product);

await session.CompleteAsync();

Migrations In Code

using mvdmio.Database.PgSQL.Migrations;

var migrator = new DatabaseMigrator(db, typeof(Program).Assembly);
await migrator.MigrateDatabaseToLatestAsync();

Embedded Schema Files

When a project references mvdmio.Database.PgSQL directly, or references another project that does, any Schemas/**/*.sql files in that project are automatically included as embedded resources.

This works through the package's build and buildTransitive MSBuild props files.

When DatabaseMigrator is constructed with multiple assemblies and the target database is empty, every assembly that contains an embedded schema.sql (or environment-specific schema.{env}.sql) has its schema applied, in the order the assemblies are passed to the constructor. Assemblies without a matching schema resource are silently skipped. All schemas run in a single transaction, and the migrations table is pre-created so that schema files using CREATE TABLE IF NOT EXISTS "mvdmio"."migrations" do not conflict. The baseline entry in mvdmio.migrations is recorded using the highest -- Migration version: <id> (<name>) header found across the applied schemas. If the database already contains migrations, no schema files are applied. When MigrateDatabaseToAsync(targetIdentifier) is used and any discovered schema's header version exceeds the target, the entire schema-first bootstrap is skipped (applying a subset would leave gaps that later migrations cannot fill).

Generated Repositories

using mvdmio.Database.PgSQL.Attributes;

[Table("public.users")]
public partial class UserTable
{
   [PrimaryKey]
   [Generated]
   public long UserId { get; set; }

   [Unique]
   public string UserName { get; set; } = string.Empty;
}

From that model, the package generates repository types and CRUD command/data types you can use in application code.

CLI Tool

If you want a command-line workflow for migrations and schema files, install the companion tool:

dotnet tool install --global mvdmio.Database.PgSQL.Tool

See ../mvdmio.Database.PgSQL.Tool/README.md.

License

MIT. See ../../LICENSE.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on mvdmio.Database.PgSQL:

Package Downloads
mvdmio.ASP.Jobs

Job scheduling library for use in ASP.NET projects.

mvdmio.HealthCheck.Client

Client library for mvdm.io HealthCheck.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.26.0 241 4/27/2026
0.25.0 352 4/19/2026
0.24.1 97 4/16/2026
0.24.0 98 4/15/2026
0.23.14 341 4/14/2026
0.23.13 95 4/14/2026
0.23.12 90 4/14/2026
0.23.11 90 4/14/2026
0.23.9 102 4/14/2026
0.23.8 114 4/11/2026
0.23.7 99 4/11/2026
0.23.5 93 4/9/2026
0.23.4 94 4/9/2026
0.23.0 97 4/9/2026
0.22.2 244 4/1/2026
0.22.0 120 3/26/2026
0.21.0 211 3/24/2026
0.20.0 112 3/11/2026
0.19.3 111 3/11/2026
0.19.0 106 3/10/2026
Loading failed