Migration in Code-First:

Entity Framework Code-First had different database initialization strategies like CreateDatabaseIfNotExists, DropCreateDatabaseIfModelChanges, and DropCreateDatabaseAlways. However, there are some problems with these strategies, for example if you already have data (other than seed data) or existing Stored Procedures, triggers etc. in your database. These strategies used to drop the entire database and recreate it, so you would lose the data and other DB objects.

Entity Framework introduced a migration tool that automatically updates the database schema when your model changes without losing any existing data or other database objects. It uses a new database initializer called MigrateDatabaseToLatestVersion.

There are two kinds of Migration:

  1. Automated Migration
  2. Code-based Migration

Learn about automated migration in the next section.