find and solve || findandsolve.com
Please wait.....

Entity Framework Core add-Migration

Entity Framework Core add-Migration


When developing applications migration is a best way to keep the database schema in sync with the EF Core model by preserving data.

Migrations are enabled by default in EF Core and managed by executing commands. If you have Visual Studio 2019 or less then 2019, you can use the Package Manager Console  to manage migrations.

In the above image, EF Core API builds the EF Core model from the domain (entity) classes and EF Core migrations will create or update the database schema based on the EF Core model. Whenever you change the domain classes, you need to run migration to keep the database schema up to date.

Some of the must  important migration commands in EF Core as given below.

PM Commanddotnet CLI CommandUsage
add-migration <your Migration Name>Add <Your migration name>Creates a migration by adding a migration snapshot.
Remove-migrationRemoveRemoves the last migration snapshot.
Update-databaseUpdateRemoves the last migration snapshot.
Script-migrationAdd <migration name>Removes the last migration snapshot.

Creating a Migration

In the first state , you should  defined the initial domain classes.There is no database for your application which can store the data from your domain classes. So, firstly, we need to create a migration as following command.

First open your 'Package Manager Console' as given image


PM> add-migration Initial

If you want to use dotnet Command Line Interface, execute the following command.

dotnet ef migrations add Initial

In the above commands, Initial is the name of a migration. This will create three files in the Migrations folder of your project, as shown below.


  1. 2020082..455_initial.cs: In the given above the main migration file which includes migration operations in the Up() and Down() methods. The Up() method includes the code for creating DB objects and Down() method includes code for removing DB objects.
  2. 2021022...5139_initial.Designer.cs: Int the above class name as  migrations metadata file which contains information used by EF Core.
  3. DataContextModelSsnapshot.cs: In the given image above the migrations metadata file which contains information used by EF Core.

Updating the Database

PM> Update-Database

CLI

> dotnet ef database update

The Update command will create the table in the  database which is based on the your DBContent and domain classes and the migration snapshot, which is created using the add-migration or add command.


Removing a Migration

We will use this command to remove the latest migration if it is not applied to the database.

PMC(Package Manager Console)

PM> remove-migration

CLI

> dotnet ef migrations remove

In the above commands they will remove the last migration and revert the model snapshot to the previous migration.

Reverting a Migration

PMC(Package Manager Console)

PM> Update-database MyFirstMigration

CLI

> dotnet ef database update MyFirstMigration.

In the given above command to reverse a migration  which  pass the name of a target migration to the update command. The target migration is the point to which you want to restore the database


Generating a SQL Script

PMC(Package Manager Console)

PM> script-migration

CLI

> dotnet ef migrations script


Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response