我正在使用此 MSDN Tutorial 在 VS2015 中运行昨天成功运行的命令 PM> Add-Migration MyFirstMigration -context BloggingContext
,但今天它给出了其他用户也指出的以下错误 here。我什至从解决方案资源管理器中删除了 Migrations 文件夹,并从 SQL Express 2014 on Win 8.1
中删除了相应的数据库,但同样的错误。即使我运行 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 - 2016 年 6 月 27 日发布的更新 3。
更新
使用命令窗口时,以下命令可以在 Windows 资源管理器的项目目录中正常工作:
> dotnet ef migrations add MyFirstMigration --context BloggingContext
> dotnet ef database update --context BloggingContext
更新 2a
以下是 project.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%" ]
}
}
@bricelam
的回复下查看我的评论。我可能会错过什么?
只需从 nuget 安装 Microsoft.EntityFrameworkCore.Tools
包:
安装包 Microsoft.EntityFrameworkCore.Tools -版本 6.0.6
您还可以使用此链接安装最新版本:NuGet package link
.NET CLI 命令:
dotnet add package Microsoft.EntityFrameworkCore.Tools
如果问题仍然存在,请尝试重新启动 Visual Studio。
尝试以下步骤:
1) 打开 project.json
文件并从 dependencies 和 tools 部分中删除所有 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 启动时识别这些工具。
确保 project.json
的 dependencies
部分引用了 Microsoft.EntityFrameworkCore.Tools
。 NuGet 不会从 tools
部分加载包管理器命令。 (见NuGet/Home#3023)
{
"dependencies": {
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
}
}
}
Microsoft.EntityFrameworkCore.Tools
也在内部依赖项部分。按照教程中的说明,Add-Migration
和 Update-Database
命令在前一天晚上工作。但是当我第二天早上对我的模型进行了微小的更改并再次尝试这些命令后尝试它们时,它们给出了我的帖子中描述的错误消息。我什至从 SQL Server 中删除了 Migration 文件夹和相应的数据库,但是在运行这些命令后我得到了相同的错误消息。我可能会错过什么?
tools
中删除它并在之后重新添加它,这解决了我的问题。只需将其移至 dependencies
将导致以下错误消息:无法执行此命令,因为 'Microsoft.EntityFrameworkCore.Tools' 未安装在项目 'src\<your project> 中。将“Microsoft.EntityFrameworkCore.Tools”添加到 project.json 中的“工具”部分。有关详细信息,请参阅 go.microsoft.com/fwlink/?LinkId=798221。
同样的问题...由dong解决了以下问题
1.) 关闭 pm manager 2.) 关闭 Visual Studio 3.) 打开 Visual Studio 4.) 打开 pm manager
似乎诀窍是在关闭 VS 之前关闭 PM Manager
从 Asp.Net Core 5.0 的角度来看
https://i.stack.imgur.com/4txtA.png
添加对数据访问层 (DAL) 项目的引用(例如:如果有对 Auth 模型的引用)
https://i.stack.imgur.com/IIhaS.png
添加对迁移项目的引用。
如果有,请添加对数据访问层 (DAL) 项目的引用。
通过在 Startup.ConfigureServices(IServiceCollection services) 中使用以下代码行,确保您已将 DbContext 派生类型添加到服务集合:
services.AddDbContext<YourDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("YourDb"),
x=>x.MigrationsAssembly("Your.Migrations")));
我也刚遇到这个问题。我关闭并打开VS2015,它“修复”了这个问题......
您必须知道您的 Entity-Framework
版本是什么。此外,您还必须检查 project.json
并控制这些部分:
在依赖项中
查看:
Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
本节:
“版本”:“1.0.0-preview2-final”,
与您的 Entity-Framework
的版本相关,您必须对此进行更改。
之后 proj.json
的第二部分在这里,在 JSON
的 Tools
部分中,您有:
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
此外,这部分代码与您的 Entity-Framework
和 Dependencies 部分中的最后一部分代码相关。
注意: 完成此问题后,您应该关闭 CMD
并重新启动 Visual Studio。
只需尝试初始化 Microsoft.EntityFrameworkCore.Tools。在 PM 中执行
C:\Users\<username>\.nuget\packages\Microsoft.EntityFrameworkCore.Tools\1.0.0-preview2-final\tools\init.ps1.
它帮助我解决了同样的问题。工具的版本可能不同。这取决于您在项目中使用的内容。
我必须做的...
1) 工具 -> Nuget 包管理器 -> 包管理器设置
2) 常规选项卡
3) 清除所有 NuGet 缓存
4)重启Visual Studio
这对我有用:从 Visual Studio 点击
工具 --> NuGet 包管理器 --> 包管理器控制台
https://i.stack.imgur.com/Br3VU.jpg
然后您可以运行 Add-Migration
,例如:
Add-Migration InitialCreate
我在 Visual Studio 2013 中遇到了这个问题。我重新安装了 NuGet 包管理器:
https://marketplace.visualstudio.com/items?itemName=NuGetTeam.NuGetPackageManagerforVisualStudio2013
我遇到了同样的问题。我的大多数项目在工具中都有相同的东西。
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
}
除了一个项目外,这对所有项目都有效。我将工具中的条目更改为
"tools": {
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
}
}
然后运行 dotnet restore
。恢复完成后,Add-Migration
工作正常。
这些是我遵循的步骤,它解决了问题
1) 将我的 Power shell 从版本 2 升级到 3
2)关闭PM控制台
3)重新启动Visual Studio
4) 在 PM Console dotnet restore 中运行以下命令
5)Add-Migration InitialMigration
有效 !!!
就我而言,我通过 Nuget 添加了依赖项:
Microsoft.EntityFrameworkCore.Tools
然后通过包管理器控制台运行:
add-migration Initial -Context "ContextName" -StartupProject "EntryProject.Name" -Project "MigrationProject.Name"
我遇到了同样的问题,发现这是解决方案文件中的 Visual Studio 版本控制问题。
我的目标是:
VisualStudioVersion = 14.0.25123.0
但我需要定位:
VisualStudioVersion = 14.0.25420.1
直接对解决方案文件进行更改后,EF Core cmdlet 开始在包管理器控制台中工作。
14.0.25421.03 Update 3
比您使用的版本还要高。
我遇到了这个问题,以前的解决方案都没有帮助我。我的问题实际上是由于我的 Windows 7 机器上的 powershell 版本过时 - 一旦我更新到 powershell 5,它就开始工作了。
我认为答案需要在 2017 年更新,因为 MS 已经进行了一些(重大)更改,此处详述。
https://github.com/aspnet/EntityFramework/issues/7053
总而言之,您现在需要在工具部分中引用 EntityFrameWorkCore.Tools.DotNet,如下所示
“Microsoft.EntityFrameworkCore.Tools.DotNet”:“1.0.0”
我还在下面发布了一个有效的 project.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%" ]
}
}
转到包管理器控制台(在 Visual Studio 中)并执行以下命令
C:\Users\
我尝试了以上所有操作,但没有运气。我下载了最新的 .net core 2.0 包并再次运行命令,它工作正常。
1.1.1
升级到 2.0
?
在我的情况下,解决方案中有一个我没有想到的错误。我尝试重建但没有奏效。这是你应该做的
关闭 Visual Studio 并重新打开。
清理解决方案并重新构建。
它将显示解决方案中的错误。
解决错误并重试。
(ps:Visual Studio 2019 .netCore 5)
尝试安装这些软件包:
EntityFrameworkCore.Tools EntityFrameworkCore.Design
解决方案对我来说一直很好,然后停止了。在对我有点混乱之后:
已清理的解决方案关闭解决方案并重新打开直接进入包管理器控制台并运行 add-migration MigrationName
我的是一个非常愚蠢的错误!
解决方案:
重新打开 Visual Studio 和克隆存储库。
不要忘记双击解决方案文件来加载它。