28

I have a project with this structure enter image description here

TooSeeWeb.Infrastructure is for migrations.

When I try to run migrations with this command

dotnet ef migrations add ExampleMigration -s ..\TooSeeWeb

I have this error

Unable to retrieve project metadata. Ensure it's an MSBuild-based .NET Core project. If you're using custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option

How I can fix this?

Eugene Sukh
  • 2,357
  • 4
  • 42
  • 86

12 Answers12

26

This is 2 years old but I was just in the same situation so it is still relevant. It's first result on Google for this error.

So I can see in your Screenshot that you are not in the standard Windows Visual Studio so I assume you are not on Windows (makes a difference in how to write file paths). Also I can see that you used ..\TooSeeWeb with a backslash.

Solution: Change all \ to a / forward slash so in your case I guess it would be:

dotnet ef migrations add ExampleMigration -s ../TooSeeWeb

For me it was working on Windows but failing on macOS (OS X) with this error:

Unable to retrieve project metadata. Ensure it's an SDK-style project. If you're using a custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option.

Additionally it gives the information (that gives a better hint):

MSBUILD : error MSB1009: Project file does not exist.

Here my more complex statement WORKING with forward slashes:

dotnet ef --startup-project ./MainProject.csproj migrations add MyMigration --context MyDbContextPostgreSQL --output-dir Migrations --project ../MyDatabasePostgreSQL/MyDatabasePostgreSQL.csproj
CodingYourLife
  • 7,172
  • 5
  • 55
  • 69
  • 1
    This should be an accepted answer. The screenshot does not mean that it's not windows (I use Rider everywhere, including Windows). In this case that was true. I have this issue on Mac, where, as author pointed out, paths uses / instead of \ – Artur Michajluk Apr 06 '22 at 16:29
  • Please see my answer https://stackoverflow.com/a/74323906/2410655. If you open up the Powershell from your startup project, it will save you lots of typing. Also, you don't have to include `.csproj` in the `-p|--project` parameter. Also... I think the OP wants to put the migrations folder under the Data folder too. So you would have to set `-o|--output-dir` to Data/Migrations instead of just Migrations. – David Liang Nov 04 '22 at 23:28
10

You have to point to your web project

 dotnet ef --startup-project ../TooSeeWeb migrations add MigrationName -c NameOfYourDBContext

More details about multi project you can find https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/projects

mateusz
  • 101
  • 1
  • 3
3

I had the same error. I solved it by including the nuget package:

Microsoft.EntityFrameworkCore.Tools
Ryanman
  • 880
  • 1
  • 8
  • 20
3

just add the path to your startup project and database project like this:

dotnet ef database update --verbose --project "src/Services/Discount/Discount.Infrastructure.Rdms" --startup-project "src/Services/Discount/Discount.Web"

The path should be relative to your current directory to the destination directory, for instance, if I change my current directory to /src the path would be Services/Discount/Discount.Infrastructure.Rdms

Manzur Alahi
  • 1,870
  • 23
  • 19
2

The problem for me was that the project was in the solution folder, so I had to specify the project path - like this:

dotnet ef migrations add InitialCreate --project SolutionName/ProjectName.csproj
Shawn J. Molloy
  • 2,457
  • 5
  • 41
  • 59
1

I don't know how this problems appeared, but since most developers come from using these commands:

dotnet ef migrations add "MigrationName" -s ../ProjectName
dotnet ef migrations remove -s ../ProjectName
dotnet ef database update -s ../ProjectName

I replaced them with these:

dotnet ef --startup-project ../ProjectName migrations add MigrationName -c DbContextName 
dotnet ef --startup-project ../ProjectName database update -c FoodTownDbContext

I think the same apply for this, but haven't tested it:

dotnet ef --startup-project ../ProjectName migrations remove
-c DbContextName
Shawn J. Molloy
  • 2,457
  • 5
  • 41
  • 59
Manguera v
  • 412
  • 2
  • 6
  • 15
1

My error was similar, but different. In fact the same error as @CodingYourLife mentions (perhaps the error wording has changed over time, or it is in fact a different error), anyway... This was the error:

Unable to retrieve project metadata. Ensure it's an SDK-style project. If you're using a custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option.

It turned out that I needed to run the dotnet ef dbcontext scaffold <list_of_options> command from the parent folder (the one with the solution file in it - not from the PROJECT folder). I used cd .. (to move up one folder) and re-ran the command, and it created my database-context and EF classes for the tables specified.

Jonno
  • 436
  • 1
  • 4
  • 13
0

first of all, your need to see the exact context name. This is needed for

dotnet ef migrations add -c <your context name>

You can see your contexts in your project by using this command:

dotnet ef dbcontext list

You will see something like below

AspNetCore.Jwt.Sample.Config.MyIntIdentityContext AspNetCore.Jwt.Sample.Config.MyIdentityContext

See... there is more than one context. Choose&copy your context's full name and paste it after -c option

dotnet ef migrations add Initial -c AspNetCore.Jwt.Sample.Config.MyIntIdentityContext

use as above☝.

and that is ok! It works well now.

possible reasons:

ray
  • 15
  • 4
Berkay
  • 301
  • 2
  • 7
0

usually I have this problem when I receive updates for dotnet sdks. ProgramFiles/dotnet/sdk. The problem is that I use an MSBuildSDKsPath system variable and this path doesn't update after dotnet sdk updates. MsBuildSDKsPath points to old sdks

If you face the same problem, and use the same system variable, that can be the solution.

Robert Tab
  • 114
  • 1
  • 5
0

I am using Entity Framework Core .NET Command-line Tools 6.0.10, and the following worked for me.

  1. Make sure you install the EF tooling globally
    dotnet tool install dotnet-ef --global
    
  2. Make sure you update it to the latest
    dotnet tool update dotnet-ef --global
    
  3. Open a Powershell off your start up project. In your case, it would be TooSeeWeb? Your Powershell should default the root to something like
    PS C:\your-user\source\repos\TooSeeWeb\src\TooSeeWeb>
    
  4. Run the following command to add a migration
    dotnet ef migrations add ExampleMigration
      -c YourDbContext
      -p ../TooSeeWeb.Infrastructure
      -o Data/Migrations
    

-p should set the project where you want the migrations to be put in.

-o is the directory you want to put files in, and it's relative to the project directory you just set with -p.

You can also use -s to set the start up project, where you have your connection string. But since I open the Powershell off the start up, I don't have to specify this, as it defaults to the current working directory.


You can do dotnet ef migrations add -h to see the whole list of optional arguments and what they do.

David Liang
  • 20,385
  • 6
  • 44
  • 70
0

In my case it was caused by EF versions that I had installed. I had Entity Framework Core .NET Command-line Tools 7.0.1 installed and the project I was trying to add the migration to, was using EF 6.

Solution was to run EntityFramework6\Add-Migration instead of Add-Migration

Grass
  • 1
0

I had a similar experience with dotnetcore 3.1, and this worked for me.

Here's the project structure of my simple app:

  • ProjectWallet (API)
  • ProjectWallet.Data (DbContext, Models, etc)
  • ProjectWallet.Tests

I have Microsoft.EntityFrameworkCore installed on ProjectWallet.Data.

Navigate to the project where your DbContext resides on the cli;

  • cd ProjectWallet.Data

Then run;

  • dotnet ef migrations add Initial --startup-project "C:\Users\PATH_TO_THE_STARTUP_PROJECT_PROJECTWALLET"

I added the double quote " " for the PATH.

I hope this works for someone.