目前仅支持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上运行EF只需按照以下步骤进行。
打开终端,进入项目文件夹,然后运行
dotnet restore
如果一切正常,您应该可以运行
dotnet ef
之后,您可以运行像这样的命令:
dotnet ef migrations add initial
dotnet ef database update
目前 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 ...
如果您正在使用.NET Core(具体来说是EF Core),您可以在Visual Studio for Mac中安装NuGet PowerShell Core控制台。
只需按照以下说明操作:
https://lastexitcode.com/blog/2019/05/05/NuGetPowerShellCoreConsoleVisualStudioForMac8-0/
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.
dotnet ef migrations add nameOfMigration --project Project/Project.csproj
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命令。
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