githubEdit

Dapper Setup

Minimal example to enable Dapper persistence for Elsa Workflows, including connection factory setup and schema responsibility notes.

This document provides a minimal, copy-pasteable example for configuring Elsa Workflows with Dapper persistence.

Prerequisites

  • .NET 8.0 or later

  • Database server (PostgreSQL or SQL Server)

  • Elsa v3.x packages

  • Schema created manually (Dapper does not manage migrations)

NuGet Packages

For PostgreSQL:

dotnet add package Elsa
dotnet add package Elsa.Dapper
dotnet add package Npgsql

For SQL Server:

dotnet add package Elsa
dotnet add package Elsa.Dapper
dotnet add package Microsoft.Data.SqlClient

When to Use Dapper

Dapper is ideal for:

  • Performance-critical scenarios requiring minimal ORM overhead

  • Fine-grained SQL control for custom query optimization

  • Existing database schemas where you want to integrate Elsa

  • Teams with strong SQL expertise who prefer direct control

Consider EF Core instead if you need:

  • Automatic migration management

  • Higher-level abstractions

  • Simpler configuration

Minimal Configuration

Program.cs

SQL Server Example

appsettings.json

PostgreSQL:

SQL Server:

Schema Creation

With Dapper, you are responsible for creating and maintaining the database schema.

PostgreSQL Schema

SQL Server and Other Databases

For SQL Server and other databases, use the official Elsa Dapper migrations package which provides complete schema management:

Repository: elsa-extensions/Elsa.Persistence.Dapper.Migrationsarrow-up-right

Installation:

Usage:

The migrations package handles schema creation and versioning for supported databases including SQL Server, PostgreSQL, and MySQL.

Note: For custom schema requirements or unsupported databases, you can use the PostgreSQL schema above as a reference and adapt it to your database's SQL dialect.

Transactions

Dapper operations participate in ambient transactions. For explicit control:

Performance Tuning

Connection Pool Configuration

Batch Operations

Dapper excels at batch operations with low overhead:

Migration Strategy

Since Dapper doesn't manage migrations, use one of these approaches:

Option 1: FluentMigrator

Option 2: DbUp

Option 3: Plain SQL Scripts

Maintain versioned SQL scripts and apply via CI/CD:

Troubleshooting

Connection Issues

Error: Connection refused or timeout

Solutions:

  • Verify database server is running

  • Check connection string format

  • Ensure network connectivity

Schema Mismatch

Error: relation "elsa.workflow_instances" does not exist

Solution: Schema must be created before running the application. Run schema creation scripts.

Performance Issues

Slow queries:

  1. Verify indexes exist

  2. Use database query analyzer (EXPLAIN ANALYZE in PostgreSQL)

  3. Check connection pool metrics


Last Updated: 2025-11-28

Last updated