82

I'm doing a project sample by using ASP.Net Core RC2 with Microsoft.EntityFramework.Core and SQLite.

I've followed this tutorial: https://damienbod.com/2015/08/30/asp-net-5-with-sqlite-and-entity-framework-7/

But, when I run this command :

dotnet ef migrations add FirstMigration

I got this error :

No executable found matching command "dotnet-ef"

Here is my project.json configuration:

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002702",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.Sqlite": "1.0.0-rc2-final"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "gcServer": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}
Casimir Crystal
  • 18,651
  • 14
  • 55
  • 76
Redplane
  • 2,621
  • 2
  • 25
  • 48
  • This announcement shows how your project.json should look like in RC2 if you are using tools: https://github.com/aspnet/Announcements/issues/172 – Pawel May 17 '16 at 23:00

13 Answers13

60

Entity Framework Core 1.0

You should just need to update the tools section of your project.json file to include this:

"Microsoft.EntityFrameworkCore.Tools": {
  "version": "1.0.0-preview1-final",
  "imports": [
    "portable-net45+win8+dnxcore50",
    "portable-net45+win8"
  ]
}

This should make the dotnet ef commands available.

Important

I should also note here that the dotnet ef commands will only be available when running them from the same directory which contains the project.json file.

Entity Framework Core 1.1
If you are having this problem again after upgrading to Entity Framework Core 1.1, be sure to replace the Microsoft.EntityFrameworkCore.Tools dependency with Microsoft.EntityFrameworkCore.Tools.DotNet version 1.1.0-preview4. There is no need to keep the imports section, either. For more information on this, see the "Upgrading to 1.1" heading under the Entity Framework Core 1.1 release announcement blog post.

Michael Freidgeim
  • 21,559
  • 15
  • 127
  • 153
Daniel Grim
  • 2,231
  • 16
  • 22
  • 22
    Not being in the folder with `project.json` was my problem, thanks! – kitsu.eb Jun 13 '16 at 17:41
  • 6
    It does not work for me. I keep getting the same error. – Nikola Schou Sep 08 '16 at 08:10
  • @NikolaSchou: In the tools section, try without the structure, i.e. just adding the version. E.g.: ` "tools": { "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final", "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" },` – Arve Hansen Sep 10 '16 at 22:04
  • @ArveHansen The problem turned out to be, that it was a class library. The tools started working after adding "buildOptions": { "emitEntryPoint": true } to project.json and adding an Main-class to the project. See this post: http://stackoverflow.com/questions/39390420/cannot-run-dotnet-ef-on-ef6-after-upgrading-to-net-core-1-0-project-files/39390729?noredirect=1#comment66108686_39390729 – Nikola Schou Sep 11 '16 at 21:10
  • Comment running from same directory as project.json was helpful. – Nakul Manchanda Sep 14 '16 at 08:47
  • 1
    @NikolaSchou You also have to reopen cmd. – Tyler Oct 15 '16 at 17:42
  • Please open the "src" folder, I had the same problem. – Adrian Mar 06 '17 at 18:28
  • 1
    @Adrian, thank you too. After a long time I come back to .net core. I faced with the old problem as the time I asked this question. So funny – Redplane Mar 29 '17 at 16:15
  • Whether you're using a csproj file or project.json, you have to be in that folder. – LexH Aug 21 '17 at 20:05
56

Entity Framework Core 1.1

Adding in on this if you're using VS2017 with the new .csproj projects without a project.json file

you need to edit the .csproj file (right click it in solution explorer and click edit whatever.csproj) and then paste this in

<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet">
      <Version>1.0.0-*</Version>
    </DotNetCliToolReference>
  </ItemGroup>

courtesy of : https://github.com/aspnet/EntityFramework/issues/7358#issuecomment-278379967

Michael Freidgeim
  • 21,559
  • 15
  • 127
  • 153
mwoa
  • 713
  • 7
  • 15
  • it works but, need to know the correct version. As i am new it was also very difficult for me to figure out the correct version. So anyone following the answer know your Microsoft.EntityFrameworkCore.Tools.DotNet version and put in version tag. i.e 2.0.3 – Saleh Enam Shohag Aug 11 '18 at 08:34
  • This really saves my day. Thx! I updated `VS2017` and it automatically updated `dotnet core CLI` to `2.0` version which is not compatible with `dotnet core 1.0`. But it seems it's the time that I should update my project `.net core 1.0` to `2.0`. – Boooooooooms Sep 04 '18 at 08:40
40

Specific to VS2017 15.3 or greater and ASP.NET CORE 2.0 or later...

Install nuget for db provider via command line or nuget package manager.

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Add following section to .csproj

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" 
    Version="2.0.0" />
</ItemGroup>

Install design time tools via commandline or nuget manager in VS2017.

dotnet add package Microsoft.EntityFrameworkCore.Design

This enables dotnet ef * at the command line in the project directory.

Enables dotnet ef * commands at the command line in the project directory,

dotnet ef migrations add Initial
dotnet ef database update Initial
dotnet ef dbcontext scaffold 
Randi Ratnayake
  • 624
  • 9
  • 22
Joe Healy
  • 5,349
  • 3
  • 32
  • 54
  • 2
    Great answer, I was looking everything and the section on adding to the 'ItemGroup' was what was really blocking for me on .NET Core 2 Web API using EF Core 2. – djangojazz Nov 25 '17 at 22:13
  • Same here. The `DotNetCliToolReference ` with Tools.DotNet was the key in my case – blfuentes Apr 03 '18 at 09:23
  • This fixed my problem, Same issue with NET Core 2 Web API using EF Core 2 – Deluq Jul 28 '18 at 15:25
32

This is a common issue when switching from .NET Core 1.0 to .NET Core 1.1+ or 2.x.

To fix that, you need to:

  • Get the Microsoft.EntityFrameworkCore.Tools and Microsoft.EntityFrameworkCore.Tools.DotNet package libraries using NuGet.
  • Manually add a reference to this package within your project.json (for .NET Core 1.0) or <projectName>.csproj (for .NET Core 1.1+ & 2.x) project configuration file.

More specifically, for .NET Core 1.0 projects, add this:

"tools": {
   "Microsoft.EntityFrameworkCore.Tools": "1.0.0"
   "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0"
 }

For .NET Core 1.1+ and .NET Core 2.x projects, add this:

<ItemGroup>
  <DotNetCliToolReference 
      Include="Microsoft.EntityFrameworkCore.Tools" 
      Version="2.0.0" />
  <DotNetCliToolReference 
      Include="Microsoft.EntityFrameworkCore.Tools.DotNet" 
      Version="2.0.0" />
</ItemGroup>

If you already have a tools json key or an <ItemGroup> element with one or more existing DotNetCliToolReference elements, just add the new ones to the existing group.

IMPORTANT: other than performing the above steps, you have to launch the dotnet ef command within the project root folder (the one containing the project file), otherwise it won't work.

For additional info and an extensive explanation of the issue you can read more on my blog post.

Samuel Liew
  • 68,352
  • 105
  • 140
  • 225
Darkseal
  • 8,378
  • 6
  • 68
  • 98
18

I think I have found the Accurate solution for the problem - dotnet : No executable found matching command "dotnet-ef"..

I am using dot net core 2.0 in VS 2017 versio 15.5.3

Cause of this error

This error is caused because the Nuget is not able to find the solution file on the location.

Solution:- Move to the directory where you have the 'Startup.cs' class

  1. I Moved to the root by adding the below command on your Package Manager Console.

    cd .\School1

Here 'School1' was my root directory of the project, and it contains my 'Startup.cs' class, it will be different in your case.

  1. Then run the command dotnet ef on Package Manager Console which will now run successfully.

Example With Pictures for Clear Understanding

  1. I got error Error when running dotnet ef. enter image description here

  2. I corrected the error by moving to the root folder with the command cd .\School1

Hope it helps my fellow Dot Net Developers.

enter image description here

yogihosting
  • 3,745
  • 2
  • 31
  • 54
12

I had to add Microsoft.EntityFrameworkCore.Tools.DotNet to work. The tools section of your project.json file will look like this:

"tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
    "Microsoft.AspNetCore.Razor.Tools": "1.1.0-preview4-final",
    "Microsoft.EntityFrameworkCore.Tools":"1.1.0-preview4-final",
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4-final"
},
4

If you met this problem and run the asp.net core with CLI tool, you may solve it by adding <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> into xx.csproj file, and dotnet restore . Then you can use dotnet ef command.

Dauphin
  • 81
  • 1
  • 3
  • I actually ran into the same issue. I was using 2.0.2 Microsoft.EntityFrameworkCore.Tools.DotNet, and update the proj file, reloaded the project, and then retested and it worked. Thanks! – Miles May 30 '18 at 00:26
1

Instead of opening a separate console window in VS Package Manager Console type and run the following commands:

Add migration

Add-Migration <migration name>

Remove last migration

Remove-Migration

Before a migration has been applied (or, to apply migration):

Update-Database

When migration has been applied:

Update-Database -Migration <previous migration> -Context <db context name>

HTH

EDIT: You may also need the following class in your MVC core project:

public class DbContextFactory : IDesignTimeDbContextFactory<NotesContext>
{
    public YourDbContext CreateDbContext(string[] args)
    {
        var builder = new DbContextOptionsBuilder<YourDbContext>();
        builder.UseSqlServer("DefaultConnection", optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(YourDbContext).GetTypeInfo().Assembly.GetName().Name));
        return new YourDbContext(builder.Options);
    }
}
Community
  • 1
  • 1
Alexander Christov
  • 7,414
  • 6
  • 36
  • 52
1

In my case dotnet ef wasn't available and not showing in the list when dotnet -h is run.

I've installed globally dotnet-ef with following command and I'm now able to use it. But still not in the list.

dotnet tool install -g dotnet-ef 
HasanG
  • 11,539
  • 29
  • 95
  • 147
0

Under visual studio 2017 i needed to run these commands from package manager console

install-package Microsoft.EntityFrameworkCore.SqlServer.Design

Scaffold-DbContext "Server=.\sqlexpress;Database=MyDb;Trusted_Connection=True;MultipleActiveResultSets=true" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model -Context "MyApp"
Kyriacos
  • 73
  • 1
  • 2
  • 5
0

I was using a separate class library project. After trying and failing all of above in package manager console. I used command prompt, and it worked! Weird. However, credit goes to this article. And if you are using separate class library project, This is your solution.

Mohsin
  • 506
  • 6
  • 13
0

In tools section add below code,

 "tools": {

    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview2-final",

    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {

      "version": "1.0.0-preview2-final",
      "imports": "portable-net45+win8+dnxcore50"
    }
  }

This format of code solved my error.

sheldonzy
  • 3,933
  • 6
  • 34
  • 65
0

By default when adding a NuGet Pkg it will be added as a PackageReference, this is wrong, so edit it manually

1- Edit .csproj file

2- change from "PackageReference":

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.1"/>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3"/>
</ItemGroup>
to:
<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.1"/>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3"/>
</ItemGroup>
Luis
  • 148
  • 1
  • 4