0
votes

I'm using VSCode and I'm struggling to add my first migration in the "multi-project solution". Usually I was doing one .csproj per project, but recently I decided to play with the .sln file and I hit the wall while doing migrations. This is my project structure:

MyApi
  - src
     - MyApi.Domain
     - MyApi.Infrastructure (this is where my DbContext is)
     - MyApi.WebApi (this is where my connection sting is)
  - tests
  - MyApi.sln

I'm trying to create the database in PostgreSQL database, so this are the packages I installed in my MyApi.Infrastructure project.

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.2">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
    <PackageReference Include="Microsoft.NETCore.App" Version="2.2.8" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.2" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.1" />
  </ItemGroup>

I'm trying to run migration while in the Infrastructure project, but I'm getting this error:

Unable to create an object of type 'MyDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

It's not a surprise, because it doesn't even know the connection string.

How should I handle migrations in such project structure?

2

2 Answers

3
votes

Try to use this in root folder:

dotnet ef --startup-project src\MyApi.WebApi\  migrations add MyMigration --project src\MyApi.Infrastructure
2
votes

Go to MyApi.Infrastructure and run command:

dotnet ef --startup-project ..\MyApi.WebApi\ migrations add First_Migration -c DBContextName