Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Add Migration in Visual Studio Code for EntityFrameworkCore

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?

like image 539
mejobloggs Avatar asked Dec 24 '22 16:12

mejobloggs


1 Answers

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
like image 126
DavidG Avatar answered Dec 29 '22 05:12

DavidG