我正在使用这个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%" ]
  }
}

当前回答

从Asp。Net Core 5.0透视图

在Migrations项目中 安装以下程序:

添加对数据访问层(DAL)项目的引用(例如:如果有对认证模型的引用)

在Startup/Api项目中 安装以下程序:

向Migrations项目添加引用。 如果您有数据访问层(DAL)项目,则添加对它的引用。

通过使用Startup中的以下代码行,确保已将DbContext派生类型添加到服务集合。ConfigureServices (IServiceCollection服务):

services.AddDbContext<YourDbContext>(options => 
                  options.UseSqlServer(Configuration.GetConnectionString("YourDb"),
                                      x=>x.MigrationsAssembly("Your.Migrations")));

其他回答

以下是我遵循的步骤,它解决了问题

1)将Power shell从版本2升级到版本3

2)关闭PM控制台

3)重启Visual Studio

4)在PM控制台运行以下命令 dotnet恢复

5) Add-Migration InitialMigration

成功了!!

试试下面的步骤:

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启动时识别了这些工具。

试试init Microsoft.EntityFrameworkCore.Tools。 在PM中执行

C:\Users\<username>\.nuget\packages\Microsoft.EntityFrameworkCore.Tools\1.0.0-preview2-final\tools\init.ps1.

它帮我解决了同样的问题。 工具的版本可能不同。这取决于你在项目中使用什么。

在我的例子中,在解决方案中有一个我没有想到的错误。 我试过重建,但没有成功。 以下是你应该做的

关闭Visual Studio并重新开放。 清理解决方案并重新构建。 它将显示解决方案中的错误。 解决错误,然后重试。

(Ps: Visual Studio 2019 .netCore 5)

我认为答案需要在2017年更新,因为微软已经做了一些(突破性的)改变,详情在这里。

https://github.com/aspnet/EntityFramework/issues/7053

总结一下,您现在需要在下面的工具部分中引用EntityFrameWorkCore.Tools.DotNet

"Microsoft. EntityFrameworkCore. Tools. DotNet" : "1.0.0"

我还发布了一个工作项目。Json文件下面,以防有人运行在问题。

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
    "Microsoft.Extensions.Configuration.Json": "1.0.1",
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    }
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0"
  },

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

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

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

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

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}