Michael Lopez
Michael Lopez

Reputation: 153

Visual Studio Mac Preview Entity Framework SQLite add migration

I've installed Visual Studio for Mac (OSX 10.12.1) today and I've been diving in quite extensively.

I wanted to try to get EntityFrameworkCore (1.1.0) to run with SQLite.

So I've created a new Console Application .NET Core and with some troubles been able to add all the necessary nuget packages. Somehow Visual Studio was not able to download the dependencies, so I had to download every dependency manually. (Maybe this solves the problem: .Net Core 1.1.0 NuGet packages fail to install in Visual Studio Mac haven't testet this yet.)

As stated in this article (https://learn.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite) I wanted to add the migration, but I couldn't find the necessary command line tool in the IDE. Did I miss something here?

Then I went on to use the .NET Core CLI to do it manually via. console. ( https://www.microsoft.com/net/core#macos). But when I execute dotnet ef migrations add init I get the following error.

No executable found matching command "dotnet-ef"

Was anyone able to get this to run successfully?

Upvotes: 4

Views: 9975

Answers (5)

eduardogoncalves
eduardogoncalves

Reputation: 173

For me this solved the issue on macOS, run this on any terminal:

dotnet tool install --global dotnet-ef

Upvotes: 0

Khushbu kadia
Khushbu kadia

Reputation: 29

Without add this ItemGroup, can't add the dbcontext scaffold connection string use of cmd, so adding this in your project (Edit .csproj) first

 <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
  </ItemGroup>

Upvotes: 0

Jzrzmy
Jzrzmy

Reputation: 331

Using VS for Mac, adding those following lines into .csproj makes the migration work for me:

<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild2-final" />

Packages will be automatically restored saving the .csproj from VS.

To run the "dotnet ef" command through the terminal, you need to be in the project directory, I mean not from the directory where the .sln file is, but from the lower level.

Note: Same trick with v1.0.1 of Tools.DotNet didn't work, I do not know why.

Upvotes: 2

miha
miha

Reputation: 3377

Visual Studio for Mac 2017 currently (April 2017) does not support adding a reference to Microsoft.EntityFrameworkCore.Tools.DotNet and returns an error:

Package 'Microsoft.EntityFrameworkCore.Tools.DotNet 1.0.0' has a package type 'DotnetCliTool' that is not supported by project 'MacMvc'.

You can edit the file manually and add the reference directly to the csproj file, as documented. Add this to your csproj file:

  <ItemGroup>
     <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
  </ItemGroup>

Then run dotnet restore to install the package. After that, you will be able to use dotnet ef migrations add NameOfMigration and dotnet ef database update scripts as per documentation.

N.B.: you must be in the project directory when executing commands.

Also see suggestion feeedback for VS 2017 for Mac:

Upvotes: 9

Krzysztof Branicki
Krzysztof Branicki

Reputation: 7837

Check if you have this section in your project.json file and add it if it's missing.

"tools": {
   "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4"
 }

This is valid for EF 1.1, previous version was using Microsoft.EntityFrameworkCore.Tools package

Upvotes: 0

Related Questions