首页 > 解决方案 > PowerShell 中的 EF6 添加迁移,在 Visual Studio 之外

问题描述

如何Add-Migration在 PowerShell 窗口中在 Visual Studio 之外运行 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.

标签: powershellentity-framework-6entity-framework-migrations

解决方案


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

PS ~/> $path = (Get-Module -Name EntityFrameworkCore).Path

对我来说,这解决了:

PS ~/> $path

C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools\2.1.1\tools\EntityFrameworkCore.psm1

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

PS ~/> Import-Module -Name $path

但是在查看文件夹(令人讨厌地不符合标准)之后,它还有一个模块清单文件(.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

脚注:这是我的 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

推荐阅读