我正在使用这个MSDN教程在VS2015中运行命令PM> Add-Migration MyFirstMigration -context BloggingContext,昨天运行成功,但今天它给出了以下错误,其他用户也在这里指出了这一点。我甚至从解决方案资源管理器中删除了Migrations文件夹,并从Win 8.1的SQL Express 2014中删除了相应的db,但同样的错误。即使我运行Add-Migration MyFirstMigration,我也会得到相同的错误:

Add-Migration : The term 'Add-Migration' is not recognized as the name of a cmdlet, function, script file, or operable program. 
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Add-Migration MyFirstMigration -context BloggingContext
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Add-Migration:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

注意:我使用的是最新版本的ASP。NET Core 1.0和VS2015 -更新3于2016年6月27日发布。

更新

当使用命令窗口时,以下命令在windows资源管理器的项目目录中工作良好:

> dotnet ef migrations add MyFirstMigration --context BloggingContext
> dotnet ef database update --context BloggingContext

更新2

以下是项目。json文件:

{
  "userSecretsId": "aspnet-ASPCore_RTM_CodeFirst_test-bef835d9-9831-41a8-bc3a-cd2f1477a880",

  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": {
      "version": "1.0.0",
      "type": "build"
    },
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    }
  },

  "tools": {
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview2-final",
      "imports": [
        "portable-net45+win8"
      ]
    }
  },

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

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

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

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

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

当前回答

试试下面的步骤:

1)开放项目。从依赖项和工具部分删除所有Microsoft.EntityFrameworkCore.Tools引用。

2)关闭包管理器控制台(PMC)并重新启动Visual Studio

3)在依赖项下添加:

 "Microsoft.EntityFrameworkCore.Tools": {
  "version": "1.0.0-preview2-final",
  "type": "build"
 }

4)在工具部分下添加

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

5)重新启动Visual Studio 2015

6)打开PMC并输入

Add-Migration $Your_First_Migration_Name$

这是因为PMC在Visual Studio启动时识别了这些工具。

其他回答

试试下面的步骤:

1)开放项目。从依赖项和工具部分删除所有Microsoft.EntityFrameworkCore.Tools引用。

2)关闭包管理器控制台(PMC)并重新启动Visual Studio

3)在依赖项下添加:

 "Microsoft.EntityFrameworkCore.Tools": {
  "version": "1.0.0-preview2-final",
  "type": "build"
 }

4)在工具部分下添加

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

5)重新启动Visual Studio 2015

6)打开PMC并输入

Add-Migration $Your_First_Migration_Name$

这是因为PMC在Visual Studio启动时识别了这些工具。

只需从nuget安装Microsoft.EntityFrameworkCore.Tools包:

安装软件包Microsoft.EntityFrameworkCore.Tools -Version 6.0.10

您也可以使用此链接安装最新版本: NuGet包链接

.NET CLI命令:

dotnet add package Microsoft.EntityFrameworkCore.Tools

如果问题仍然存在,请尝试重新启动Visual Studio。

我瞎了,但我试图在“开发人员Powershell”中做到这一点,但需要打开“包管理器控制台”,而不是选择项目,您的迁移所在的位置。此外,通过“解决方案资源管理器”上下文菜单中的“设置为启动项目”将项目设置为活动。

另外,请确保将连接字符串设置为您的命令。否则,您需要将连接添加到机器环境变量。

如果你在DbContext中指定它:

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql("MyConnectionStringName");            
    }

因此,“MyConnectionStringName”应该设置为环境变量名,值-连接字符串。

希望,会帮助别人

我也遇到了同样的问题。我的大多数项目都有同样的工具。

"tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
}

除了一个项目,其他项目都很好。我把工具栏的条目改成了

"tools": {
    "Microsoft.EntityFrameworkCore.Tools": {
    "version": "1.0.0-preview2-final",
    "type": "build"
  }
}

然后运行dotnet恢复。恢复完成后,Add-Migration工作正常。

我遇到了这个问题,以前的解决方案都帮不了我。我的问题实际上是由于我的Windows 7机器上的powershell版本过时了——一旦我更新到powershell 5,它就开始工作了。