GreenyMcDuff
GreenyMcDuff

Reputation: 3622

Visual Studio Code Entity Framework Core Add-Migration not recognized

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 EntityFramework 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 VSCode editor itself?

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

Upvotes: 52

Views: 69667

Answers (5)

Gururaj
Gururaj

Reputation: 157

If any one trying to create Migrations from cmd and facing issues like this.

first try to run:

dotnet tool install --global dotnet-ef --version=(specify)

then try adding migration:

dotnet ef migrations add DbInitialMigration

then update the database:

dotnet ef database update

Upvotes: 0

raghu.warrier
raghu.warrier

Reputation: 21

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

    <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.2" />  
    </ItemGroup>
    
  2. in Bash/Command prompt

    dotnet restore
    
  3. after that

    dotnet ef migrations add MyDbInitialMigration
    

Upvotes: 2

Dronacharya
Dronacharya

Reputation: 1281

The correct format to add a new migration is:

dotnet ef migrations add yourMigrationName

and to update database is:

dotnet ef database update

Upvotes: 80

Rafael Almeida
Rafael Almeida

Reputation: 391

You need to add:

dotnet tool install --global dotnet-ef

Upvotes: 34

Chris Kooken
Chris Kooken

Reputation: 33870

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

Upvotes: 3

Related Questions