Creating a new migration

Every time your model changes, you need to add a new migration to your project. Follow these steps to learn how to do this::

  1. Open the Package Manager Console (Tools | NuGet Package Manager | Package Manager Console).
  2. Type and run the following command:
    Add-Migration [Migration Name] -p [Project Name]
  1. 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).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.124.145