Run dotnet tool restore to make the dotnet-ef command available

Viewed 6808

I'm having a quite strange situation with Visual Studio and EF migrations in Publish Profile. Given:

  • Visual Studio 2019 v16.4
  • .NET Core project. Targets .NET Core 3.1
  • EF 3.1
  • Azure publish profile

I've created migrations in local project and when trying to enumerate migrations in publish profile, I get

Run dotnet tool restore to make the dotnet-ef command available

enter image description here

I don't know what this error means, because dotnet-ef tools seems installed:

PM> dotnet --version
3.1.101
PM> dotnet ef --version
Entity Framework Core .NET Command-line Tools
3.1.1
PM> dotnet ef dbcontext list --json --project MyUIProject
Build started...
Build succeeded.
[
  {
     "fullName": "MyDataProject.MyDbContext",
     "safeName": "MyDbContext",
     "name": "MyDbContext",
     "assemblyQualifiedName": "MyDataProject.MyDbContext, MyDataProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
  }
]
PM> dotnet tool restore


Restore was successful.
PM>

Last line doesn't change anything. I can use PS commands such as Add-Migration, Update-Database, or cmd commands such as dotnet ef migrations add, dotnet ef database update. dotnet-ef <...> work as well. The only place where they don't work -- publish profile settings. I can't enable checkbox to run migrations on publish.

Data project has referenced these EF-related packages:

<...>
<PackageReference Include="microsoft.aspnetcore.Identity.EntityFrameworkCore" Version="3.1.1" />
<PackageReference Include="microsoft.EntityFrameworkCore" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Data.SqlClient" Version="4.8.0" />
<...>

UI project references only Design:

<...>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<...>

There are lots of similar questions around, like this: Cannot list Entity Framework Migrations in Visual Studio 2019 due to dotnet ef dbcontext --json failure

It seems their common issue is that they didn't have tools installed, because dotnet ef commands are no longer parts of .NET Core SDK and it is not my case, since local EF tools are installed and work from command line.

3 Answers

I found 2 ways

  1. try to run visual studio as administrator.

  2. if (1.) does not work, in the project enter dotnet new tool-manifest, dotnet tool install dotnet-ef, dotnet tool restore.

Description

Somehow visual studio cannot access C://Users/AlexanderK/.nuget where global packages are installed.There can be some software on our pc that blocks visual studio to access globally installed packages. I'm not sure how nuget works but npm for nodejs works as search in the locally and globally installed packages. So in our case for visual studio there is no installed locally dotnet-ef while cannot access it globally.

In (1.) we run as admin and try to give the rights to visual studio to access the globally installed packages.

In (2.) we install dotnet-ef locally so nuget will find the package locally for sure.

More about dotnet tool commands - Official Docs

I recommend you to use (1.) because most PCs wont have that problem and installing packages like dotnet-ef locally is not good idea when you have it globally..

Check which .NET Core SDK platform versions you have installed... 32-bit or 64-bit or both.

If you have both, I think VS2019 will use whatever it finds first, which may not be the one you have EF Core tools installed in.

I had the same problem (after an update of visual studio 2019), on my pc there is this problem but in another pc no. The files are all equal (checked via git), VS2019 cache something somewhere and this is bad.

1 Check the default connection string is ok.

2 Check if dotnet ef is installed. In core 3.1 you must install: dotnet tool install -g dotnet-ef

3 Close VS, delete the folder <yourproject>/Properties (backup it if you want), open VS, clean solution and import the publish file again.

Related