43

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. 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.
    – Alexan
    Jan 8, 2017 at 19:12

4 Answers 4

64

The correct format to add a new migration is:

dotnet ef migrations add yourMigrationName

and to update database is:

dotnet ef database update
6
  • Thank you - found the official documentation that explained this based on your answer. Jan 8, 2017 at 19:37
  • 2
    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?
    – Matt Welke
    Sep 20, 2017 at 21:46
  • 2
    For those who want to check the official documentation: learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/…
    – Ozkan
    Oct 28, 2018 at 15:11
  • But for solutions with many projects, how to specify one project to make add migration or update database on it?
    – Zeinab
    May 29, 2019 at 23:54
  • 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
    – Paully
    Aug 16, 2020 at 16:24
21

You need to add:

dotnet tool install --global dotnet-ef
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
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
  • 1
    Add reference in .csproj file for Microsoft.EntityFrameworkCore.Tools.DotNet May 8, 2018 at 18:08
  • You can use the edit link under your post to make an update.
    – LarsTech
    May 8, 2018 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.