Introduction to Entity Framework Migration
Entity Framework Migration is a tool that automatically updates the schema of the database while there is any change in the Model but without any loss of existing data or any database objects. The new database initializes called MigrateDatabaseToLatestVersion is used by the EF Migration. Migration allows you to make changes in the data model without any drop or re-creation of the database.
What is Entity Framework Migration?
The Entity Framework Migration allows you to make any alterations in the data model and it organizes and updates the database schema changes made to the production without any database drop or re-creation of the database. The Migrations offers the set of tools that enables you to create the database initially which works with the EF Model and it generating the migrations to keep the track of alterations that we make to the EF Model and also it keeps the database up to date with the changes made at the schema.
The Entity Framework Migration creates the EF Model from the Domain or entity classes and the EF Migration creates or updates the database schema based on EF Model. When you make any changes in the domain classes we need to execute the migration to maintain the database schema update.
Entity Framework Migration commands
The Entity Framework Migration allows you to make any alterations in the data model and it organizes and updates the database schema changes made to the production without any database re-creation or drop of the database.
The Entity Framework migrations are the set of executed commands which we can run either in PMC (Package Manager Console) or the CLI (Command Line Interface). Let’s see the important commands of Entity Framework Migration,
Adding new Migration – this command is used to create a migration by including a migration snapshot, let’s see the following commands
In Package Manager Console, add-migration<migration name>
In CLI > dotnet ef migrations add <migration name>
Remove Migration – this command is used to remove the last migration snapshot, let’s see the following commands
In Package Manager Console, remove-migration
In CLI > dotnet ef migration remove
Update Database – this command is used to update the db schema on the basis of the last migration snapshot, let’s see the following commands
In Package Manager Console, update-database
In CLI > dotnet ef database update
Script Migration – this command is used to generate the SQL Script by using the entire migration snapshots, let’s see the following commands
In Package Manager Console, script-migration
In CLI > dotnet ef migrations script
Let’s see the add migration,
- PMC Command: it is used to add the new migration
- CLI Command: is dotnet ef migrations add
While we creating or altering the domain classes initially we need to create the migration, for the creation of new migration we need to use by the command that is add-migrationin other ways we can also use the command line for adding migration dotnet ef migrations. Just open the Console Manager package and execute the following commands as follows,
add-migration “initial” // this is to the package console manager
Dotnet ef migrations add “initial” // dot net cli
Entity Framework Migration three entities
The Entity Framework offers the three entities which approach the database schema migration, let’s see the three approaches as follows
- Database first– the Database First is the reverse engineer approach where the existing database generates the data model and which maps it entirely.
- Code first– the code first approach defines the data model in the application code and then finally uses the code for generating the new database.
- Model first– the model first approach generates the database by using the data model created with the EF Designer.
When we compare the code first and the database first approaches, both are comparatively based on the third approach called model-first.
Adding and Creating the Migration
To add the new migration initially we need to define the initial domain classes here there is no database for the application so it can store the data from the domain classes. Instead of that, we need to create migration. Initially open the PMC (Package Manager Console) from Toolsà NuGet Package Manager à Package Manager Console in VS and run the commands as follows,
You were using the dotnet Command Line Interface, run the command as follows,
- dotnet ef migrations add MyFirstMigration
In above those commands, the name MyFirstMigration is the name of the Migration, once we created the migration it automatically creates three files in the Migration Folder of the project as shown,
- <timestamp>_<Migration Name>.cs it is the main migration file contains the operation of migration in the methods of Up() and Down(). The Up () method contains the coding for the creation of Database Objects and the Down () method contains the coding for eliminating/ removing the Database Objects.
- <timestamp>_<Migration Name>.cs it is a migrations metadata file that includes the information used by Entity Framework Core.
- <contextclassname> ModelSnapshot.cs it is the snapshot of the current model, this file is used to establish the changes made while generating the subsequent migration.
Once we create with the Migration Snapshot next we have to create the Database, Creating or Updating the Database. To create and update the database follow the commands,
In Package Manager Console, PM>Update-Database
In CLI > dotnet ef database update
The update command will generate the database on the basis of the domain classes and the context classes and also with the migration snapshot with the help of using the add-migration or the add command.
Where this is the first migration so it creates the table called the _EFMigrationsHistory which stores the name of all migrations so that it will be applied to the database.
To remove the migration
For removing the last migration, use the command to remove the lastly created migration file which reverts the model snapshot. Let’s follow the commands in both ways as follows,
In Package Manager Console, PM>remove-migration
In CLI > dotnet ef migrations remove
The above commands remove the lastly created migration and it reverts the snapshot model to the previous migration.
In this article we have learned about the procedure of Entity Framework Migration, which is a tool automatically, manages the database schema without any data loss. Hope the article helps you to understand the usages of Entity Framework Migration.
This is a guide to Entity Framework Migration. Here we discuss the introduction, What is Entity Framework Migration, commands, Adding, and Creating the Migration. You may also have a look at the following articles to learn more –