No executable found matching command "dotnet-ef"

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%" ]
}
}
54246 次浏览

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.

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"
},

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

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"

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.

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.

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.

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);
}
}

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

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.

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

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>

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