Skip to content

Manage migrations

Niray Mak edited this page Jun 10, 2021 · 3 revisions

Entity Framework Migrations

Windows

In Windows you can use Visual Studio to do EF migrations.

  1. Open Package Manager Console (PMC) ( Tools -> NuGet Package Manager -> Package Manager Console)
  2. As startup project select 01_API.
  3. As Default Project in Package Manager Console, choose 06_Data.

image

  1. After your changes are done, you can create a migration by using the syntax beneath in the PMC
    Add-Migration [YourMigrationName] -C ApplicationDbContext

image

  1. CHECK YOUR MIGRATION. A .cs file will be generated and automatically opened. Since the file is generated it is a good idea to check if the file is generated correctly. If the file is not generated succesfully, see step 7.

  2. Execute Migration. The migration can be executed with the command beneath in the PMC
    Update-Database -C ApplicationDbContext

  3. If you want to remove the migration before step 6, use the PMC commands.
    Remove-Migration -C ApplicationDbContext

If you want to remove an applied migration, consider making a new migration. Especially if the migration is already in develop.

Linux

While working on Dex in a Linux enviroment, you'll be probably be using Rider-like IDEs and there will be no "Package Manager Console" to do the migration operations.

To be able to overcome this drawback, you should follow these steps;

  • Go to this link EF Core and install the entity framework core tools by following the steps (If you haven't installed dotnet core alread, go ahead and follow these steps within this link Dotnet Core)
  • After a successful installation, run this command to add a migration; dotnet ef migrations add <MIGRATION_NAME> -s <STARTUP_PROJECT> -p <DB_CONTEXT_PROJECT> --context <DB_CONTEXT_PROJECT>.<CONTEXT_NAME> (Here is an example command that I've used; dotnet ef migrations add AddLikesFieldToProject -s API -p Data --context Data.ApplicationDbContext)
  • If you couldn't find the db contexts in your solution, run this command; dotnet ef dbcontext list -p <DB_CONTEXT>

Credits go to aintabb for this Wiki.