Run dotnet tool restore to make the dotnet-ef command available
Asked Answered
H

3

5

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.

Hanafee answered 25/2, 2020 at 9:23 Comment(3)
While I'm not using Windows for development, I can theorize that it might have something to do with the user privileges (I had similar problem in Linux). If the tools are installed only for specific user profile or, for instance, the directory where dotnet ef resides is missing from the PATH environment variable of the user which executes the publish, then you might get this.Vibrissa
It worked until I have installed .NET 3.1 SDK. Trying to remove and reinstall doesn't solve the problem. Again, I can run these commands from cmd, package manager console and they work. You can see the working output from these commands in my question. The only place where they seem are missing -- publish settings dialog.Hanafee
I'm suddenly having exactly the same issue as of today. Haven't really changed anything in my project apart from a bit of HTML, but I did update VS2019. However, I have the same problem when cloning the same repo to a laptop with an old version of VS2019.Lynda
C
6

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..

Continuator answered 29/4, 2020 at 20:27 Comment(2)
Sorry for late response. Tried to run VS as administrator and loaded existing project -- same issue.Hanafee
Try (2.) and reportContinuator
D
0

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.

Delftware answered 6/3, 2020 at 23:30 Comment(4)
I have installed .NET 3.1 x64 version of SDK. Today I installed .NET 3.1 SDK x86. No changes.Hanafee
Did you run both from the command line and check EF tools was installed in both?Delftware
I ran dotnet ef in x64 and in x86 cmd consoles. I see an unicorn logo in both and they show same version: 3.1.1. It seems that EF commands are installed in both architectures.Hanafee
That's pretty conclusive... looks like this isn't the issue then. Worked for me when I had a similar issue. Sorry that it didn't help. I'd keep poking around the installation/path stuff... it really smells like VS looking in the wrong spot.Delftware
H
0

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.

Humpbacked answered 22/5, 2020 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.