Postgres + Dapper - Create database if it doesn't exist on startup with C# and ASP.NET Core
Tutorial built with .NET 7.0, Dapper 2.0 and PostgreSQL
I recently built a .NET 7 API that uses the Dapper micro ORM to connect to a Postgres database. Dapper is a lightweight ORM that doesn't include database migrations so I needed to come up with another solution for auto generating the database tables on API startup.
I decided to keep things simple and add a custom Init()
method to my DataContext
class and call it once on app startup, the method executes some SQL to setup the PostgreSQL database and tables if they don't exist.
Below are the pieces of code from the API project that are responsible for creating the database and tables on startup. The full project and documentation is available at .NET 7.0 + Dapper + PostgreSQL - CRUD API Tutorial.
PostgreSQL Data Context
The data context class is used to connect to the Postgres database with ADO.NET and return an IDbConnection
instance.
The Init()
method creates the PostgreSQL database and tables if they don't already exist, it is executed once on API startup from the Program.cs file.
The Dapper ExecuteAsync()
extension method is called to execute SQL statements on the database.
namespace WebApi.Helpers;
using System.Data;
using Dapper;
using Microsoft.Extensions.Options;
using Npgsql;
public class DataContext
{
private DbSettings _dbSettings;
public DataContext(IOptions<DbSettings> dbSettings)
{
_dbSettings = dbSettings.Value;
}
public IDbConnection CreateConnection()
{
var connectionString = $"Host={_dbSettings.Server}; Database={_dbSettings.Database}; Username={_dbSettings.UserId}; Password={_dbSettings.Password};";
return new NpgsqlConnection(connectionString);
}
public async Task Init()
{
await _initDatabase();
await _initTables();
}
private async Task _initDatabase()
{
// create database if it doesn't exist
var connectionString = $"Host={_dbSettings.Server}; Database=postgres; Username={_dbSettings.UserId}; Password={_dbSettings.Password};";
using var connection = new NpgsqlConnection(connectionString);
var sqlDbCount = $"SELECT COUNT(*) FROM pg_database WHERE datname = '{_dbSettings.Database}';";
var dbCount = await connection.ExecuteScalarAsync<int>(sqlDbCount);
if (dbCount == 0)
{
var sql = $"CREATE DATABASE \"{_dbSettings.Database}\"";
await connection.ExecuteAsync(sql);
}
}
private async Task _initTables()
{
// create tables if they don't exist
using var connection = CreateConnection();
await _initUsers();
async Task _initUsers()
{
var sql = """
CREATE TABLE IF NOT EXISTS Users (
Id SERIAL PRIMARY KEY,
Title VARCHAR,
FirstName VARCHAR,
LastName VARCHAR,
Email VARCHAR,
Role INTEGER,
PasswordHash VARCHAR
);
""";
await connection.ExecuteAsync(sql);
}
}
}
.NET 7 Program
The .NET 7 Program file configures dependency injection, initializes the PostgresSQL database, configures the HTTP request pipeline and starts the API.
Postgres database initialization
The Init()
method of the data context is executed once on startup to ensure the PostgreSQL database and all required tables exist.
using System.Text.Json.Serialization;
using WebApi.Helpers;
using WebApi.Repositories;
using WebApi.Services;
var builder = WebApplication.CreateBuilder(args);
// add services to DI container
{
var services = builder.Services;
var env = builder.Environment;
services.AddCors();
services.AddControllers().AddJsonOptions(x =>
{
// serialize enums as strings in api responses (e.g. Role)
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
// ignore omitted parameters on models to enable optional params (e.g. User update)
x.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
// configure strongly typed settings object
services.Configure<DbSettings>(builder.Configuration.GetSection("DbSettings"));
// configure DI for application services
services.AddSingleton<DataContext>();
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<IUserService, UserService>();
}
var app = builder.Build();
// ensure database and tables exist
{
using var scope = app.Services.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<DataContext>();
await context.Init();
}
// configure HTTP request pipeline
{
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
// global error handler
app.UseMiddleware<ErrorHandlerMiddleware>();
app.MapControllers();
}
app.Run("http://localhost:4000");
Need Some PostgreSQL Help?
Search fiverr for freelance PostgreSQL developers.
Follow me for updates
When I'm not coding...
Me and Tina are on a motorcycle adventure around Australia.
Come along for the ride!