52

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
2
  • 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. Commented Jan 8, 2017 at 19:03
  • 1
    from console you should use the following: dotnet ef migrations add MyDbInitialMigration. I think the same should be in VS terminal. Commented Jan 8, 2017 at 19:12

5 Answers 5

81

The correct format to add a new migration is:

dotnet ef migrations add yourMigrationName

and to update database is:

dotnet ef database update
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you - found the official documentation that explained this based on your answer.
Hope you're still around to see this comment... I'm having trouble finding the official docs on the new way to do this with the dotnet CLI. I actually came to this StackOverflow page after Add-Migration didn't work. Mind sharing where you found this in the docs?
For those who want to check the official documentation: learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/…
But for solutions with many projects, how to specify one project to make add migration or update database on it?
From Terminal window, cd into the project root folder before running the command. A new Migrations folder will appear in that project folder with all the migration artifacts as expected. Here is an updated link to MS docs: learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet
|
34

You need to add:

dotnet tool install --global dotnet-ef

Comments

3

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

Comments

2
  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
    

2 Comments

Add reference in .csproj file for Microsoft.EntityFrameworkCore.Tools.DotNet
You can use the edit link under your post to make an update.
0

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

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.