dotnet / dotnet/efcore

When using separate migrations assembly, the output path is not relative to target project

Open
#32,752 1 comment 0 reactions 0 assignees View on GitHub
area-migrations area-tools customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

## File a bug

I just moved all migrations into a separate assembly as guided by the [docs](https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/projects?tabs=dotnet-core-cli).

Project structure:

```
src/
StartupProject
DbContextProject
MigrationsProject
```

I use the following command inside `src/StartupProject` to generate idempotent script and compiled model:

```
dotnet ef migrations script -i -p ../MigrationsProject -o ./Scripts/Idempotent.sql && dotnet ef dbcontext optimize --no-build -p ../DbContextProject -o ./Model
```

Using the above command, the compiled model output will be in `src/DbContextProject/Model`. However, the idempotent script is written to `src/StartupProject/Scripts/Idempotent.sql` (relative to startup project, not target project). I would have expected the file to be written to `src/MigrationsProject/Scripts/Idempotent.sql` since the target project was set using the `-p` flag just as with the compiled model.

Edit 2024-01-15: I checked the code base and the difference between add and script is that

`dotnet migrations add` combines ProjectDir with Path: https://github.com/dotnet/efcore/blob/09b51d43b01268c15a5a2bc31c8356435d573b32/src/EFCore.Design/Design/Internal/MigrationsOperations.cs#L80
`dotnet migrations script` combines WorkingDir with Path: https://github.com/dotnet/efcore/blob/09b51d43b01268c15a5a2bc31c8356435d573b32/src/ef/Commands/MigrationsScriptCommand.cs#L33

## Workaround

This works, but I would have preferred that both the `dotnet ef migrations` and `dotnet ef dbcontext optimize` with the `-o` flag would both output into a path relative to the target project.

```
dotnet ef migrations script -i -p ../MigrationsProject -o ../MigrationsProject/Scripts/Idempotent.sql && dotnet ef dbcontext optimize --no-build -p ../DbContextProject -o ./Model
```

### Include provider and version information

EF Core version: 8.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.