Every time your model changes, you need to add a new migration to your project. Follow these steps to learn how to do this::
- Open the Package Manager Console (Tools | NuGet Package Manager | Package Manager Console).
- Type and run the following command:
Add-Migration [Migration Name] -p [Project Name]
- The initial migration in the GiveNTake application was created as follows:
Add-Migration InitialMigration -p GiveNTake
After the command is complete, a new file will be added to your project under a folder with the name Migrations. The file is named in the [Creation Time]_[Migration Name] format, for example, 20180219101736_InitialMigration.cs.
This file contains a class that derives from Microsoft.EntityFrameworkCore.Migrations.Migration and contains two methods:
- Up(MigrationBuilder migrationBuilder): Contains the operations needed to apply the migration. The operations are applied through the migrationBuilder methods.
- Down(MigrationBuilder migrationBuilder): Contains the operation needed to revert the migration.
Besides the migration file, EF Core generates a file with a name in the [DbContextName]ModelSnapshot format. This file contains a snapshot of the model that will help EF Core to detect changes that were made in your model without connecting to the database.
Now that you have a migration file (or files) ready, I'll explain how you can add code that will run the migrations automatically on startup (if needed).