How to change the output folder for migrations with asp.net Core?

asp.netDatabaseEntity Frameworkasp.net CoreCommand Line-Interface

asp.net Problem Overview


Does anyone know how to change the output directory of the following command?

dotnet ef  migrations add Initial --context EsportshubApi.Models.ApplicationDbContext

I tried to add the option:

--content-root-path 'Migrations/Identity' 

But that doesn't do anything. There is a --data-dir option as well and something else with directory. But none of them is the output for migrations.

My problem is that I have 2 DbContexts so I want their migrations to be separated.

asp.net Solutions


Solution 1 - asp.net

dotnet ef migrations add Initial --context EsportshubApi.Models.ApplicationDbContext -o YourFolderPath

dotnet ef migrations add

Adds a new migration.

Arguments:

Argument Description
<NAME> The name of the migration.

Options:

Option Short Description
--output-dir <PATH> -o The name of the migration.
--namespace <NAMESPACE> -n The namespace to use for the generated classes. Defaults to generated from the output directory. Added in EF Core 5.0.

Also here are the common options you can use with this command.

Source

Solution 2 - asp.net

For Package Manager Console run this command:

PM> Add-Migration 001 -OutputDir "Data/Migrations"

My structure is:

.AspCoreProject
  -Data
    -Migrations
       20190721162938_001.cs
       MainDbContextModelSnapshot.cs

Update:

For removing last migration use:

PM> Remove-Migration

Note: If the migration is already applied to the database, then you will get this error:

> The migration '20190721162938_001' has already been applied to the database. Revert it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.

Then run:

PM> Remove-Migration -Force

If your migration is not the last migration. first, rollback to the migration you need by Update-Database then delete all migration classes after that migration.

PM> Update-Database -Migration 001

This will revert all migrations after 001

Solution 3 - asp.net

In EF Core 5.0, you are free to move Migration files and changes their namespace manually. New migrations are created as siblings of the last migration. Alternatively, you can specify the directory at generation time as follows:

> .Net core CLI

dotnet ef migrations add InitialCreate --output-dir Your/Directory

> PowerShell

Add-Migration InitialCreate -OutputDir Your\Directory

EF Core 5.0 documentation

Solution 4 - asp.net

You just need to use -o Or --output option with your command,

To do so, you need to explore to your root project folder, eg: C:\project\SampleAPi
and use this command

dotnet ef  migrations add DbInitial --context SampleAPi.Infrastructure.DbContext -o Infrastructure/Migrations

and then

dotnet ef database update

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDenLilleMandView Question on Stackoverflow
Solution 1 - asp.netJérôme MEVELView Answer on Stackoverflow
Solution 2 - asp.netFereydoon BarikzehyView Answer on Stackoverflow
Solution 3 - asp.netMalik HaseebView Answer on Stackoverflow
Solution 4 - asp.netHarry SarshoghView Answer on Stackoverflow