40

I have Visual Studio for Mac and I'm trying to learn Xamarin with Azure using the following tutorial: https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter3/server/

At some point, I have to enable EF migrations. The tutorial says: Go to View -> Other Windows -> Package Manager Console.

Unfortunately there is no Package Manager Console in Visual Studio for Mac... so how do you handle things like enable-migrations, add-migration or update-database on the Mac?

franswa
  • 836
  • 1
  • 9
  • 21
  • 2
    The Visual Studio `Package Manager Host` is not currently supported `macOS` using the PowerShell beta and thus trying to install/init `EntityFramework.psm1` will fail as running powershell will result in a `ConsoleHost` and thus trying to run `Import-Module` on the EntityFramework PS module will fail. The migration cmds are fairly thin wrappers over the entity framework apis and you can convert them to a C# cmd-line app fairly easily, but it is fair easier to spin up a Windows VM on the Mac... sucks, but those are the two options available today : https://stackoverflow.com/a/20382226/4984832 – SushiHangover Jul 28 '17 at 22:34
  • It is more than just supporting the Package Manager Host. There is a separate NuGet extensions addin that adds a PowerShell console. The problem is that, at least with EF 6, the PowerShell commands are Visual Studio specific. When I integrated support for the EF 6 in SharpDevelop all the PowerShell commands needed to be rewritten to work with the host IDE. EF 7 has provided cross platform commands that work on the command line using the dotnet cli. However PowerShell integration is a another problem. – Matt Ward Jul 29 '17 at 10:23
  • Hello this is currently supported on Mac please change the answer. Regards ;) – G Clovs Nov 03 '22 at 11:39

6 Answers6

56

This is currently supported on Mac.

First you need to install dotnet-ef

dotnet tool install --global dotnet-ef

To install a specific version of the tool, use the following command:

dotnet tool install --global dotnet-ef --version 3.1.4

Add the "dotnet-ef" tools directory on the PATH environment variable.

export PATH="$PATH:/Users/'your user folder'/.dotnet/tools"

Open a command line, go to the project folder, and run

dotnet restore

If everything is fine, you should be able to run

dotnet ef

After that you can run commands like:

dotnet ef migrations add initial

dotnet ef database update

PS: Your solution should not be executing when the dotnet ef command line is trying to run!!!

For People who are not convinced, here a demo of succeed!!! For People who are not convinced, here a demo of succeed!!!

G Clovs
  • 2,442
  • 3
  • 19
  • 24
35

To run EF on Mac just follow the following.

Open a command line, go to the project folder, and run

dotnet restore

If everything is fine, you should be able to run

dotnet ef

After that you can run commands like:

dotnet ef migrations add initial

dotnet ef database update

Community
  • 1
  • 1
J T
  • 391
  • 3
  • 3
  • 2
    FYI... If dotnet EF doesn't you may need to install dotnet tools as it was removed in .net core 3.0 – JordanGW Jan 09 '20 at 23:16
  • 10
    I had to run the install like this -- dotnet tool install -g dotnet-ef. There is more detail on this at: https://learn.microsoft.com/en-us/ef/core/get-started/?tabs=netcore-cli – Taylor Maxwell Apr 04 '20 at 17:48
  • I needed to update to latest dotnet tool update --global dotnet-ef – somedirection Jun 08 '22 at 16:22
19

This is not currently supported with Visual Studio for Mac out of the box.

There is a NuGet extensions addin that adds a PowerShell console to Visual Studio for Mac. This is available from the Extensions Manager, and provides a NuGet Package Manager Console window, available from the View - Other Windows menu. This allows you to run the EF PowerShell commands that you can run in Visual Studio on Windows.

If you are using Entity Framework 7 (or what they are calling Entity Framework Core) then you should be able to use the commands with the .NET Core command line.

dotnet ef migrations ...

If you are using Entity Framework 6 then you would need to find another way to call the migrations instead of using PowerShell. Entity Framework 6 has PowerShell commands that are specific to Visual Studio. They were ported to SharpDevelop but involved re-writing them to work with that IDE.

Matt Ward
  • 47,057
  • 5
  • 93
  • 94
  • https://meta.stackoverflow.com/questions/359302/merge-tags-visual-studio-mac-and-visual-studio-for-mac?noredirect=1#comment531840_359302 – Hans Passant Nov 13 '17 at 22:02
5

If you are using .NET Core (specifically EF Core), you can install the NuGet PowerShell Core Console in Visual Studio for Mac'.

Just follow the instructions described at:

https://lastexitcode.com/blog/2019/05/05/NuGetPowerShellCoreConsoleVisualStudioForMac8-0/

Chipo Hamayobe
  • 877
  • 11
  • 13
  • This solution Works with Visual Studio 2022 for Mac version 17.5 and EF Core 7.0.5. I fully recommend it. – mhld Jun 11 '23 at 15:36
0

Mac Users

If you have N-Layer Architecture and multiple projects, make sure you're running command on the right path (possibly under the startup project path)

I was having issues because of a wrong path

dotnet ef database update

Of course you have to install ef first

dotnet tool install --global dotnet-ef --version {VERSION_NUMBER}

then export path

export PATH="$PATH:/Users/firatkeler/.dotnet/tools"

And do not forget to install db locally if your settings are pointing to a local db. Install and try to connect it first, then apply dotnet ef database update command.

jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
Ahmet Firat Keler
  • 2,603
  • 2
  • 11
  • 22
0

I had these errors

When i ran this command

dotnet ef migrations add nameOfMigration

The error that I got was this

No project was found. Change the current working directory or use the --project option.

Then add at the end the solution of the project that I had in the root

dotnet ef migrations add nameOfMigration --project project.sln

But it gave me another error

error MSB4057: The target "GetEFProjectMetadata" does not exist in the project.

I realized that it had to be inside the project folder and the csproj file

dotnet ef migrations add nameOfMigration --project Project/Project.csproj

And for me this was my solution