102

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.

Salek
  • 449
  • 1
  • 10
  • 19
DenLilleMand
  • 3,732
  • 5
  • 25
  • 34

5 Answers5

149
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 directory used to output the files. Paths are relative to the target project directory. Defaults to "Migrations".
--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

Jérôme MEVEL
  • 7,031
  • 6
  • 46
  • 78
  • 4
    Do you know how to set the folder in C#? I'd prefer not to use an extra argument each time. – Levi Fuller Apr 07 '17 at 05:57
  • 2
    In the previous EntityFramework version, you were able to set the migrations folder in the `Configuration()` method and the `dotnet ef` commands compile the application so it's entirely possible you would be able to set the migration folder using C#. – Levi Fuller Apr 07 '17 at 07:12
  • Sorry I forgot about that. It's been a while I didn't touch this – Jérôme MEVEL Apr 07 '17 at 07:13
  • 52
    Also fun fact, after you set the `--output` directory once e.g. `./Config/Migrations`, future migrations are automatically created in the folder even without the argument. – Levi Fuller Apr 07 '17 at 07:14
  • 1
    @LeviFuller interesting – Konrad Sep 07 '18 at 10:47
  • @LeviFuller nice but it would be nice if we could define that somewhere like dotnet-tools.json or even in csproj – Dživo Jelić Mar 17 '22 at 18:30
79

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

fbarikzehy
  • 4,885
  • 2
  • 33
  • 39
  • 1
    could you add the remove command as well `Remove-Migration 001 -OutputDir "Data/Migrations"` not working neither does `Remove-Migration 001"` – Seabizkit Jan 22 '20 at 10:18
13

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

Package Manager Console

Add-Migration InitialCreate -OutputDir Your\Directory

EF Core 5.0 documentation

Malik Haseeb
  • 471
  • 9
  • 20
4

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
Harry Sarshogh
  • 2,137
  • 3
  • 25
  • 48
0

I had the same issue. I was able to solve by replacing relative path character '\' with '/'.

Worked syntax:

dotnet ef migrations add InitialCreate --project Infrastructure --startup-project MyProject.API --output-dir Persistence/Migrations

Please notice - I changed 'Persistence\Migrations' to 'Persistence/Migrations' by replacing '\' with '/'

This is what I initially tried (in mac os):

dotnet ef migrations add InitialCreate --project Infrastructure --startup-project MyProject.API --output-dir Persistence\Migrations

The above command created 'PersistenceMigrations' folder under the Infrastructure project. But the expected folder structure was Persistence\Migrations.

Note: 'InitialCreate' is the migration name.

abolfazl sadeghi
  • 2,277
  • 2
  • 12
  • 20
Suresh
  • 14
  • 4