3
votes

I'm trying to build a DotNetCore WebApi in Visual Studio Code with EntityFrameworkCore.

I've been reading a number of tutorials and I always get stuck at the point where it says:

Run Add-Migration InitialCreate

I get the following error:

The term 'add-migration' is not recognized as the name of a cmdlet, function, script file, or operable program

Other tutorials say I should run this command dotnet ef migrations add InitialCreate

Which gives me the error: No executable found matching command "dotnet-ef"

These are the references in my csproj:

<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3"/>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="1.1.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.1.0-preview4-final"/>

How can I add a migration?

2
Are you running the command in the same directory as the proj file?DavidG
no dotnet-ef but dotnet ef Also, ensure you're running dotnet ef from the directory containing the project.jsontchelidze
@tchelidze dotnet ef will call dotnet-ef, that's perfectly normal.DavidG
Last comment may be usefultchelidze
Does your proj file contain a DotNetCliToolReference entry for Microsoft.EntityFrameworkCore.Tools.Dotnet?DavidG

2 Answers

8
votes

To be able to use the command line CLI to manage migrations, you need to have the tools as part of your project. Make sure you have something like this in your project:

<ItemGroup>
    <DotNetCliToolReference 
        Include="Microsoft.EntityFrameworkCore.Tools.Dotnet" 
        Version="1.0.1" />
</ItemGroup>

Next from the command line, make sure you are in the same directory as the csproj file and run this:

dotnet ef migrations add InitialCreate
1
votes

to make the "dotnet-ef" command available in Visual Studio Code,Install the dotnet ef tool by running the preceding command.

dotnet tool install --global dotnet-ef

Adding a migration:

dotnet ef migrations add MigrationName

try executing

dotnet ef database update