如何在 Visual Studio for Mac 中启用迁移

43
我有Visual Studio for Mac,想用以下教程学习使用Xamarin和Azure: https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter3/server/。在某个环节上,我需要启用EF迁移。教程中说:转到“视图”->“其他窗口”->“程序包管理器控制台”。遗憾的是,Visual Studio for Mac 中没有“程序包管理器控制台”...那么您如何处理诸如“enable-migrations”、“add-migration”或“update-database”等操作?

2
Visual Studio的Package Manager Host在使用PowerShell beta时不支持macOS,因此尝试安装/初始化EntityFramework.psm1将失败,因为运行powershell会导致ConsoleHost,因此尝试在EntityFramework PS模块上运行Import-Module也将失败。迁移命令是对实体框架API的薄包装器,您可以将它们相对容易地转换为C#命令行应用程序,但在Mac上启动Windows VM更加容易。很遗憾,这是今天可用的两个选项:https://dev59.com/8mIj5IYBdhLWcg3wUjvG#20382226 - SushiHangover
这不仅仅是支持包管理器主机。还有一个单独的NuGet扩展插件,添加了一个PowerShell控制台。问题在于,至少对于EF 6而言,PowerShell命令是特定于Visual Studio的。当我在SharpDevelop中集成对EF 6的支持时,所有PowerShell命令都需要重新编写以适应宿主IDE。EF 7提供了跨平台命令,可以使用dotnet cli在命令行上工作。然而,PowerShell集成是另一个问题。 - Matt Ward
你好,目前仅支持在Mac上使用,请修改答案。谢谢 ;) - G Clovs
@GClovs的回答对我有用 :) - undefined
6个回答

64

目前仅支持Mac系统。

首先需要安装dotnet-ef:

dotnet tool install --global dotnet-ef

要安装特定版本的工具,请使用以下命令:

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

将“dotnet-ef”工具目录添加到PATH环境变量中:

export PATH="$PATH:/Users/'你的用户名'/.dotnet/tools"

打开命令行,进入项目文件夹,然后运行:

dotnet restore

如果一切正常,你应该可以运行:

dotnet ef

之后就可以执行如下命令:

dotnet ef migrations add initial

dotnet ef database update

注意:当尝试运行dotnet ef命令行时,你的解决方案不应该在执行中!

对于那些还没有被说服的人,这里有一个成功演示的截图:

对于那些还没有被说服的人,这里有一个成功演示的截图


这应该是我在 Mac 上,运行得非常好。你必须在解决方案的 racine 使用它。 - G Clovs
哈哈,你在逗我吧,命令行是存在的。你应该找到一种让它工作的方法。我每天都在使用它。 - G Clovs
当你的项目没有运行时,你必须运行它。 - G Clovs
1
这对我在 MacBook Pro M1 上运行成功。虽然不需要将“dotnet-ef”添加到 PATH 环境变量中,但它仍然可以正常工作! - George Feakes
这篇帖子值得更多的点赞。 - cbutler
显示剩余2条评论

35

在Mac上运行EF只需按照以下步骤进行。

打开终端,进入项目文件夹,然后运行

dotnet restore

如果一切正常,您应该可以运行

dotnet ef

之后,您可以运行像这样的命令:

dotnet ef migrations add initial

dotnet ef database update


2
请注意...如果dotnet EF不可用,您可能需要安装dotnet工具,因为它在.net core 3.0中已被删除。 - JordanGW
10
我需要这样运行安装:dotnet tool install -g dotnet-ef。关于此更多详细信息,请参见:https://learn.microsoft.com/en-us/ef/core/get-started/?tabs=netcore-cli - Taylor Maxwell
我需要更新到最新的dotnet工具更新 --global dotnet-ef。 - somedirection

19

目前 Visual Studio for Mac 不支持此功能。

可以通过 NuGet 扩展插件在 Visual Studio for Mac 中添加 PowerShell 控制台。该插件可从扩展管理器中获取,并提供一个 NuGet 包管理器控制台窗口,可从“视图 - 其他窗口”菜单中访问。这使您能够在 Visual Studio for Windows 上运行的 EF PowerShell 命令。

如果您使用 Entity Framework 7(或称为 Entity Framework Core),则应该能够使用 .NET Core 命令行 运行命令。

dotnet ef migrations ...

如果您正在使用Entity Framework 6,则需要找到另一种调用迁移的方法,而不是使用PowerShell。Entity Framework 6具有特定于Visual Studio的PowerShell命令。它们被移植到SharpDevelop,但涉及重新编写它们以使其与该IDE配合工作。

https://meta.stackoverflow.com/questions/359302/merge-tags-visual-studio-mac-and-visual-studio-for-mac?noredirect=1#comment531840_359302 - Hans Passant

5

这个解决方案适用于Visual Studio 2022 for Mac版本17.5和EF Core 7.0.5。我强烈推荐使用它。 - mhld

2
我遇到了这些错误
当我运行这个命令时
dotnet ef migrations add nameOfMigration

我遇到的错误是这个。
No project was found. Change the current working directory or use the --project option.

然后在最后添加我在根目录中的项目解决方案。
dotnet ef migrations add nameOfMigration --project project.sln

但是它给了我另一个错误。
error MSB4057: The target "GetEFProjectMetadata" does not exist in the project.

我意识到它必须在项目文件夹和csproj文件内部。
dotnet ef migrations add nameOfMigration --project Project/Project.csproj

对我来说,这是我的解决方案。

我有一台搭载M1芯片的Mac电脑。 - Roberto E. M. Austin

0

Mac 用户

如果您使用 N 层架构和多个项目,请确保在正确的路径上运行命令(可能是在启动项目路径下)

我曾因为错误的路径而遇到问题

dotnet ef database update

当然,你首先必须安装 ef

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

然后导出路径

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

如果您的设置指向本地数据库,请不要忘记在本地安装数据库。先安装并尝试连接它,然后再应用dotnet ef database update命令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接