Updated July 10, 2023
Introduction to Entity Framework Migration
Entity Framework Migration is a tool that automatically updates the schema of the database when there is any change in the Model but without any loss of existing data or any database objects. The EF Migration uses the new database initialized called MigrateDatabaseToLatestVersion. Migration lets you change the data model without dropping or re-creating 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 a set of tools that enables you to create the database initially, which works with the EF Model, and it generates the migrations to keep track of alterations that we make to the EF Model and also 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 executed commands that 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:
1. Adding new Migration – This command creates 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>
2. Remove Migration – This command removes the last migration snapshot.
Let’s see the following commands:
In Package Manager Console, remove-migration
In CLI > dotnet ef migration remove
3. Update Database – This command updates the db schema based on the last migration snapshot.
Let’s see the following commands:
In Package Manager Console, update-database
In CLI > dotnet ef database update
4. Script Migration – This command generates 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:
Add-Migration
- PMC Command: It is used to add the new migration.
- CLI Command: Is dotnet ef migrations add.
While creating or altering the domain classes, initially, we need to create the migration; for the creation of new migration, we need to use the command that 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 – Database first is the reverse engineer approach where the existing database generates the data model and which maps it entirely.
- Code first – 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 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; the application has no database, 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,
PMàadd-migration MyFirstMigration
You were using the dotnet Command Line Interface; run the command as follows:
- dotnet ef migrations add MyFirstMigration
In the above commands, the name MyFirstMigration is the name of the Migration; once we create the migration, it automatically creates three files in the Migration Folder of the project as shown,
- <timestamp>_<Migration Name>.cs is the main migration file that contains the operation of migration in the methods of Up() and Down(). The Up () method contains the coding for creating Database Objects, and the Down () method contains the coding for eliminating/ removing the Database Objects.
- <timestamp>_<Migration Name>.cs is a migrations metadata file that includes the information used by Entity Framework Core.
- <contextclassname> ModelSnapshot.cs 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, we have to create the Database, Create 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 based on the domain classes and the context classes, and the migration snapshot with the help of using the add-migration or the add command.
Where this is the first migration, 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 last created migration and revert the snapshot model to the previous migration.
Conclusion
In this article, we have learned about the procedure of Entity Framework Migration, which is a tool that automatically manages the database schema without any data loss. Hope the article helps you to understand the usages of Entity Framework Migration.
Recommended Articles
We hope that this EDUCBA information on “Entity Framework Migration” was beneficial to you. You can view EDUCBA’s recommended articles for more information.