32
votes

I've used yoman to generate an ASP.Net Core Web API application via the Visual Studio Code Editor. For reference, I followed this tutorial here

The API works fine. However, I am trying to use Entity Framework Core Migrations with SQL Server. When I type the following into the Visual Studio Code Terminal:

Add-Migration MyDbInitialMigration

I get the following message:

'Add-Migration' is not recognized as an internal or external command, operable program or batch file.

I have the Microsoft.EntityFrameworkCore.Tools: 1.1.0-preview4-final dependency installed. I did this using the .Net Core Project Manager (Nuget) extension.

In Visual Studio 2015 this command works fine from the Package Manager Console.

I assume that using Visual Studio Code's Terminal is the problem. But does anyone know how I can use EF Core Migrations from within the VS Code editor itself?

Thanks for any help.

Solution

Running the dotnet ef migrations add InitialCreate command yielded the following error:

No executable found matching command "dotnet-ef"

To solve this I needed to install the following dependency, AND add it to the tools section:

Microsoft.EntityFrameworkCore.Tools.DotNet
5
To further your suspicions, can you confirm the same doesn't work in a console? Please also post your full project.json or packages file.Jon Douglas
from console you should use the following: dotnet ef migrations add MyDbInitialMigration. I think the same should be in VS terminal.Alexan

5 Answers

47
votes

The correct format to add a new migration is dotnet ef migrations add yourMigrationName

and to update database is dotnet ef database update

9
votes

You need to add:

dotnet tool install --global dotnet-ef
3
votes

Im working on Mac, so Ruby is installed by default. My EF commands required lots of extra parameters --project, --startup-project etc. This was a pain to type every time, so I used rake to make this easier.

In my project root, I added a file called rakefile with these contents:

desc "Add Migraion"
task :'add-migration' do
    ARGV.each { |a| task a.to_sym do ; end }  
    puts ARGV[1]
    sh "dotnet ef migrations add " + ARGV[1] + " --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj "
end

desc "Remove Migraion"
task :'remove-migration' do
    ARGV.each { |a| task a.to_sym do ; end }  
    puts ARGV[1]
    sh "dotnet ef migrations remove --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj"
end

desc "Update Database"
task :'update-database' do
    ARGV.each { |a| task a.to_sym do ; end }  
    puts ARGV[1]
    sh "dotnet ef database update --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj"
end

Then at the command line, I run these commands:

rake add-migration <migrationName>
rake remove-migration
rake update-database
1
votes

Step 1 First we need to add reference in *.csproj file in the following way

Step 2 in Bash/Command propt

dotnet restore

Step 3

dotnet ef migrations add MyDbInitialMigration

0
votes

In this case, the project must be checked first. Migration cannot be initiated if there is any error in the project.