5

How can I run the EF6 Add-Migration outside Visual Studio, in a PowerShell window?

When I try to run it, there is this error message:

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.
4
  • Hi, I know it's a long time since you posted this. But have you managed to solve it? Commented Jan 15, 2020 at 10:02
  • No, I'm still stuck using it from Visual Studio Commented Jan 15, 2020 at 10:26
  • I have managed to load it in PS, but now I get this error: The term 'Get-Project' is not recognized as the name of a cmdlet Commented Jan 15, 2020 at 10:54
  • Hi! how did you manage to do it? @TheFreeman Thanks! Commented Apr 20, 2021 at 14:56

2 Answers 2

3

You need to ensure the module is loaded. Because this is part of the VS environment from that console window, it loads some different modules by default. You can resolve this by opening up your console in VS, and use

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

For me, this resolved to:

PS ~/> $path

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

So you can then take that path and import it in a regular powershell window:

PS ~/> Import-Module -Name $path

But after looking at the folder (which annoyingly doesn't follow standards), it also has a module manifest file (.psd1), which is what you should import instead:

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

Footnote: this is how my VS install initializes its console:

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the help! I could not import EntityFramework.6.2.0\tools\EntityFramework.psd1 (it is not EF Core but EF 6). It said: Import-Module : The name of the current Windows PowerShell host is: 'ConsoleHost'. The module '...\Packages\ EntityFramework.6.2.0\tools\EntityFramework.psd1' requires the following Windows PowerShell host: 'Package Manager Host'. It seems MS ensured these should only run from within VS... I'm new to PowerShell. How could I renamed the PowerShell host?
You can either remove that line from the manifest or import the module file itself.
your answer is entirely about EFCore, however the question was about EF6..
0

You can install Microsoft.EntityFramework.Tools of compatible version from NuGet Package Manager. That may solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.