Entity Framework Core Mastery

Managing Complex EF Core Migrations (CI/CD)

1 Views Updated 5/4/2026

Managing Complex EF Core Migrations (CI/CD)

In a local development environment, running dotnet ef database update is harmless. However, in a professional production environment, automatically applying migrations during application startup is extremely dangerous. One locked table or a failed index creation can crash your entire multi-server cluster. Mastering the CI/CD pipeline for migrations is essential for Senior Developers.

1. Avoid "Database.Migrate()" in Production

Calling _context.Database.Migrate() inside Program.cs sounds convenient, but if you have 10 servers behind a load balancer, all 10 servers will simultaneously try to apply the exact same SQL schema changes when they boot up, leading to race conditions and database corruption.

2. Generate SQL Scripts for the DBA

The gold standard for Enterprise deployment is generating an Idempotent SQL Script. This script checks if each migration has already been applied before executing it, making it safe to run multiple times.

# Generates a script from the very first migration to the latest
dotnet ef migrations script --idempotent --output deploy.sql

3. Using Migration Bundles (.NET 6+)

Migration Bundles are the modern way to deploy schema changes. They compile your migrations into a single, standalone executable that doesn't even require the .NET SDK to be installed on the target server. This is perfect for Docker containers and DevOps pipelines.

# Create the bundle (executable file)
dotnet ef migrations bundle --output efbundle.exe

# Execute it in your CD pipeline (e.g., Azure DevOps or GitHub Actions)
./efbundle.exe --connection "YourConnectionString"

4. Interview Mastery

Q: "How do you handle a 'Breaking Change' migration where you need to split a 'Name' column into 'FirstName' and 'LastName' without losing any existing production data?"

Architect Answer: "This requires a 3-step 'Expand and Contract' strategy. First, you add the new `FirstName` and `LastName` columns but keep them nullable. Second, you run a data migration (SQL or C#) to copy and split the existing `Name` data into the new columns. Third, once the data is safely moved and verified, you release a second migration to remove the old `Name` column and mark the new columns as NOT NULL. This ensures the application remains online and functional throughout the entire transition period without any data loss."

Entity Framework Core Mastery
1. Foundations & Architecture
Introduction to Object Relational Mapping (ORM) Entity Framework Core Architecture & Providers Setup and DbContext Integration Code-First vs Database-First Approaches Reverse Engineering Existing Databases (Scaffolding)
2. Code-First Modeling
Entity Conventions & Data Annotations The Fluent API Deep Dive (OnModelCreating) Primary Keys, Composite Keys, & Guids Required Properties & Database Defaults Value Conversions (Enums & Strongly Typed IDs)
3. Relational Architecture
One-to-Many Relationships & Foreign Keys One-to-One Relationships (Dependent Entities) Many-to-Many Relationships & Navigation Properties Owned Entity Types (Value Objects) Table-per-Hierarchy (TPH) Inheritance
4. Data Querying & LINQ
Basic LINQ Queries & IQueryable Execution Tracking vs No-Tracking Queries (Performance) Eager Loading vs Explicit Loading (Include) Lazy Loading Pitfalls & Proxies Client vs Server Evaluation Parsing
5. Manipulating Data (CUD)
Adding, Updating, and Removing Entities The ChangeTracker and Entity States Disconnected Entities in Web APIs Batch Updates and Deletes (.NET 7+)
6. Advanced Performance & Scale
Concurrency Tokens and Optimistic Locking Raw SQL Queries and Views (FromSqlRaw) Compiled Queries for High Throughput Interceptors (Logging & Auditing Data Changes) DbContext Pooling Mechanisms Managing Complex EF Core Migrations (CI/CD)