Lesson 38 of 40 Data Access Intermediate 45 min

Database Design & Migrations

Design robust database schemas, write zero-downtime migrations, handle multi-tenancy patterns, and optimize indexes for production.

Part 1: Zero-Downtime Migrations

StepActionSafe?
1Add nullable column✅ Online
2Deploy app using new column
3Backfill column data✅ Batched
4Add NOT NULL constraint✅ After backfill
5Remove old column✅ After old code removed

Part 2: Index Strategy

// EF Core index configuration
modelBuilder.Entity<Order>()
  .HasIndex(o => new { o.CustomerId, o.Status })
  .HasFilter("[Status] != 'Completed'") // Partial index
  .HasDatabaseName("IX_Orders_Customer_ActiveStatus");

Part 3: Multi-Tenancy Patterns

PatternIsolationCost
Row-level (TenantId column)LowLow
Schema-per-tenantMediumMedium
Database-per-tenantHighHigh
EF Core + Finbuckle.MultiTenant makes row-level easy with auto query filters.

Part 4: Migration Squashing

After many migrations, squash them for performance:
# Remove all migration files
# Create a single "Initial" migration from current schema
dotnet ef migrations add InitialCreate --output-dir Data/Migrations

# Mark existing databases as already applied
dotnet ef database update InitialCreate
VISUAL STUDIO 2026 MADE EASY
Recommended Book

VISUAL STUDIO 2026 MADE EASY

Build real applications with C#, VB.NET, Python, JavaScript, C++, and .NET 10. A practical companion for mastering Visual Studio 2026 step by step.