如何用 asp.net 核心更改迁移的输出文件夹?

有人知道如何更改以下命令的输出目录吗?

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

我试图添加这个选项:

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

但那没用。还有一个 --data-dir选项和其他带有目录的选项。但它们都不是迁移的输出。

我的问题是,我有2个 DbContext,因此我希望将它们的迁移分开。

79746 次浏览
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

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

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

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