Visual Studio 外部 PowerShell 中的 EF6 添加迁移

nvi*_*rth 5 powershell entity-framework-6 entity-framework-migrations

如何Add-Migration在 Visual Studio 之外的 PowerShell 窗口中运行 EF6?

当我尝试运行它时,出现以下错误消息:

Add-Migration : The term 'Add-Migration' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Run Code Online (Sandbox Code Playgroud)

The*_*le1 3

您需要确保模块已加载。因为这是该控制台窗口中 VS 环境的一部分,所以它默认加载一些不同的模块。您可以通过在 VS 中打开控制台来解决此问题,然后使用

PS ~/> $path = (Get-Module -Name EntityFrameworkCore).Path
Run Code Online (Sandbox Code Playgroud)

对我来说,这解决了:

PS ~/> $path

C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools\2.1.1\tools\EntityFrameworkCore.psm1
Run Code Online (Sandbox Code Playgroud)

因此,您可以采用该路径并将其导入到常规 powershell 窗口中:

PS ~/> Import-Module -Name $path
Run Code Online (Sandbox Code Playgroud)

但在查看该文件夹后(令人烦恼的是,它不符合标准),它还有一个模块清单文件 ( .psd1),您应该导入该文件:

PS ~/> Set-Location -Path 'C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools\2.1.1\tools'
PS /Program Files/dotnet/sdk/NuGetFallbackFolder/microsoft.entityframeworkcore.tools/2.1.1/tools/> Import-Module -Name EntityFrameworkCore.psd1
Run Code Online (Sandbox Code Playgroud)

脚注:这就是我的 VS 安装初始化其控制台的方式:

Import-Module 'C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2017\ENTERPRISE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\NUGET\Modules\NuGet\NuGet.psd1'
$__pc_args=@(); $input|%{$__pc_args+=$_}; & 'C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools\2.1.1\tools\init.ps1' $__pc_args[0] $__pc_args[1] $__pc_args[2]; Remove-Variable __pc_args -Scope 0
Run Code Online (Sandbox Code Playgroud)

  • 您的答案完全是关于 EFCore,但问题是关于 EF6.. (5认同)