Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 727
DenLilleMand Avatar asked Nov 19 '16 18:11

DenLilleMand


People also ask

How do I change migration Assembly?

By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project."

How do you Unapply a migration in asp net core with ef core?

Delete the row corresponding to your migration that you want to unapply (Say "yes" to the warning, if prompted). Run "dotnet ef migrations remove" again in the command window in the directory that has the project. json file. Alternatively, run "Remove-Migration" command in the package manager console.

How do I delete migration after update-database?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.


2 Answers

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

like image 82
Jérôme MEVEL Avatar answered Oct 07 '22 02:10

Jérôme MEVEL


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

like image 44
Fereydoon Barikzehy Avatar answered Oct 07 '22 02:10

Fereydoon Barikzehy