The Problem
Database migrations are one of the most anxiety-inducing parts of any deployment. Traditional migration frameworks require you to maintain a separate set of migration files, each describing a delta from the previous schema version. Over time, these files accumulate, become hard to review, and create a linear dependency chain that makes branching and merging a nightmare.
Worse, applying migrations in production often requires downtime or complex blue-green deployment strategies. A single bad migration can lock tables, corrupt data, or leave your schema in an inconsistent state that's difficult to recover from.
What if your C# models were the single source of truth for your database schema? What if the ORM could inspect your models at startup, compare them against the live database, and apply only the necessary changes \u2014 safely, incrementally, and without downtime?
Ingredients
Noundry.Tuxedo
vlatest
Core ORM with Dapper extensions
Noundry.Tuxedo.Bowtie
vlatest
Automatic schema migration engine
Noundry.Tuxedo.Cufflink
vlatest
Schema change auditing and logging
Npgsql
vlatest
PostgreSQL ADO.NET data provider
PostgreSQL
vany
PostgreSQL database server
Bowtie is the schema management companion to Tuxedo. It reads the attributes on your model classes and generates the DDL needed to bring your database schema into alignment with your code. It handles table creation, column additions, index management, and default values.
Combined with the Cufflink auditing extension, you can also track every schema change for compliance and debugging.
Preparation
Install the Bowtie package alongside Tuxedo:
dotnet add package Noundry.Tuxedo
dotnet add package Noundry.Tuxedo.Bowtie
dotnet add package Noundry.Tuxedo.Cufflink
dotnet add package Npgsql
Bowtie works by scanning assemblies for classes decorated with the [Table] attribute. At application startup, it compares the discovered models against the existing database schema and generates ALTER statements to reconcile any differences.
The key principle is additive-only migrations: Bowtie will add new tables, add new columns, create new indexes, and set default values. It will never drop tables or columns automatically \u2014 destructive changes require explicit action, keeping your production data safe.
The Code
Here's how to configure Bowtie in your application startup and how your models drive the schema automatically. The migration runs during application startup, ensuring your database is always in sync with your code.
using System.Data;
using Npgsql;
using Noundry.Tuxedo.Bowtie;
using Noundry.Tuxedo.Bowtie.Attributes;
using Noundry.Tuxedo.Contrib;
var builder = WebApplication.CreateBuilder(args);
// Register the database connection
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddScoped<IDbConnection>(_ => new NpgsqlConnection(connectionString));
var app = builder.Build();
// Run Bowtie migrations at startup
using (var scope = app.Services.CreateScope())
{
var connection = scope.ServiceProvider.GetRequiredService<IDbConnection>();
connection.Open();
var migrator = new BowtieMigrator(connection);
// Scan the current assembly for [Table] models
migrator.AddAssembly(typeof(Program).Assembly);
// Apply migrations — additive only, safe for production
var result = await migrator.MigrateAsync();
if (result.TablesCreated.Any())
Console.WriteLine($"Created tables: {string.Join(", ", result.TablesCreated)}");
if (result.ColumnsAdded.Any())
Console.WriteLine($"Added columns: {string.Join(", ", result.ColumnsAdded)}");
if (result.IndexesCreated.Any())
Console.WriteLine($"Created indexes: {string.Join(", ", result.IndexesCreated)}");
}
// Define your models — Bowtie reads these attributes
[Table("products")]
public class Product
{
[ExplicitKey]
[Column("id")]
public Guid Id { get; set; } = Guid.NewGuid();
[Column("name", MaxLength = 200)]
public string Name { get; set; } = string.Empty;
[Column("price")]
[DefaultValue(0)]
public decimal Price { get; set; }
[Column("sku", MaxLength = 50)]
[Index("IX_Products_Sku", IsUnique = true)]
public string Sku { get; set; } = string.Empty;
[Column("is_available")]
[DefaultValue(true)]
public bool IsAvailable { get; set; } = true;
[Column("created_at")]
[DefaultValue("CURRENT_TIMESTAMP", IsRawSql = true)]
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
[Column("category_id")]
[Index("IX_Products_CategoryId")]
public Guid CategoryId { get; set; }
}
// Adding a new property here is all you need —
// Bowtie will add the column on next startup
// [Column("description")]
// public string? Description { get; set; }
Plating
Noundry Bowtie eliminates the migration file ceremony entirely. Your C# models are the single source of truth for your database schema, and Bowtie keeps the two in sync automatically on every deployment.
The additive-only approach means you can deploy with confidence. New tables and columns are created automatically, indexes are managed declaratively, and destructive changes are never applied without your explicit approval. This makes Bowtie safe for production use even in continuous deployment pipelines.
Combined with Cufflink for schema change auditing, you get full visibility into every DDL change that Bowtie applies, giving your team the confidence to deploy multiple times per day without fear of database drift.
Was this recipe helpful?
Chef George
.NET Architect & Head Chef
25+ years crafting enterprise C# solutions. Creator of the Noundry platform. Passionate about clean architecture, cloud-agnostic deployments, and code that cooks.