Skip to main content
ModernAPI

Database Migrations

EF Core migrations, schema evolution, and deployment strategies

GuidesIntermediate• 12 min read
Migration Workflow
Follow these steps for safe database schema evolution in ModernAPI
1
Create Migration
2
Review Changes
3
Test Migration
4
Apply to Database
5
Verify Schema
What are EF Core Migrations?
Code-first database schema management with version control

Key Concepts

  • Migration: A versioned set of database schema changes
  • Model Snapshot: Current state of your EF Core model
  • Up Methods: Apply changes to move forward
  • Down Methods: Reverse changes for rollback
  • Migration History: Tracks applied migrations in __EFMigrationsHistory table

Benefits

  • • Version-controlled schema changes
  • • Reproducible deployments
  • • Safe rollback capabilities
  • • Team collaboration support
  • • Environment synchronization

ModernAPI Features

  • • PostgreSQL optimized migrations
  • • snake_case naming conventions
  • • Audit column configurations
  • • Index and constraint management
  • • Seed data integration
Basic Migration Workflow
Standard process for creating and applying migrations

Step-by-Step Process

1

Modify Your Model

Update entities, add new properties, or create new entities

// Add new property to User entity public string PhoneNumber { get; set; } = string.Empty;
2

Create Migration

Generate migration file with descriptive name

dotnet ef migrations add AddUserPhoneNumber --project ModernAPI.Infrastructure --startup-project ModernAPI.API
3

Review Generated Code

Inspect migration file for accuracy and safety

// Check Migrations/20240101000000_AddUserPhoneNumber.cs migrationBuilder.AddColumn<string>( name: "phone_number", table: "users", nullable: false, defaultValue: "");
4

Apply Migration

Update database schema

dotnet ef database update --project ModernAPI.Infrastructure --startup-project ModernAPI.API
Next Steps
Continue learning about ModernAPI development

Related Guides

  • • Adding New Entities
  • • API Endpoint Creation
  • • Testing Strategies
  • • Deployment Workflows

Advanced Topics

  • • Multi-tenant migrations
  • • Zero-downtime deployments
  • • Database sharding strategies
  • • Performance optimization

Tools & References

  • • EF Core documentation
  • • PostgreSQL best practices
  • • Migration testing tools
  • • Monitoring solutions