无法使用 Entity Framework 和 Visual Studio 2015 添加迁移

Posted

技术标签:

【中文标题】无法使用 Entity Framework 和 Visual Studio 2015 添加迁移【英文标题】:Unable to add migration using Entity Framework and Visual Studio 2015 【发布时间】:2015-10-17 08:32:54 【问题描述】:

我刚刚安装了 Visual Studio 2015 Express 版本,从 Visual Studio 2013 Express 升级而来。我现在无法使用代码优先迁移更新我在我的一个项目中使用的数据库。我所做的只是从名为“Prospect”的类中名为“field”的属性中删除 [Required] 数据注释。

我尝试将 Entity Framework 升级到 6.2.3 并完全重新安装,但都没有成功,我目前使用的是 6.1.2 版本。

在 Visual Studio 2013 中,“添加迁移”命令运行良好,但尝试在 Visual Studio 2015 中添加迁移时,包管理控制台中会产生以下错误;

PM> Add-Migration remove_required_field_from_prospect 找不到

键入 [NuGet.VisualStudio.IVsPackageInstallerServices]。确保 加载包含此类型的程序集。在 D:\Repos\PDintel\PDintel\packages\EntityFramework.6.1.2\tools\EntityFramework.psm1:1004 字符:5 + $packageInstallerServices = $componentModel.GetService([NuGet.VisualStudio.I ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (NuGet.VisualStu...stallerServices:TypeName) [], RuntimeException + FullyQualifiedErrorId : TypeNotFound 您不能在空值表达式上调用方法。在 D:\Repos\PDintel\PDintel\packages\EntityFramework.6.1.2\tools\EntityFramework.psm1:1006 字符:5 + $vsPackage = $packageInstallerServices.GetInstalledPackages() | ? $_.Id -eq ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId:InvokeMethodOnNull Join-Path:无法将参数绑定到参数“路径”,因为它为空。在 D:\Repos\PDintel\PDintel\packages\EntityFramework.6.1.2\tools\EntityFramework.psm1:713 字符:28 + $toolsPath = 加入路径 $installPath 工具 +~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand 加入路径:无法将参数绑定到参数“路径”,因为它是 空值。在 D:\Repos\PDintel\PDintel\packages\EntityFramework.6.1.2\tools\EntityFramework.psm1:780 字符:74 + $utilityAssembly = [System.Reflection.Assembly]::LoadFrom((Join-Path $ToolsP ... + ~~~~~~~ + CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand 您不能在空值表达式上调用方法。在 D:\Repos\PDintel\PDintel\packages\EntityFramework.6.1.2\tools\EntityFramework.psm1:781 字符:5 + $dispatcher = $utilityAssembly.CreateInstance( + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId:InvokeMethodOnNull Join-Path:无法将参数绑定到参数“路径”,因为它为空。在 D:\Repos\PDintel\PDintel\packages\EntityFramework.6.1.2\tools\EntityFramework.psm1:810 字符:20 +(加入路径 $runner.ToolsPath EntityFramework.PowerShell.dll), + ~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand

如果您需要更多信息,请询问!

任何帮助将不胜感激!

更新 每当我尝试使用实体框架执行任何操作时,似乎都会出现此错误,即使是“启用迁移”也会触发它。

【问题讨论】:

我不是 100% 确定,但该错误看起来像是 nuget 包管理器本身的问题。安装VS后,您是否检查是否有任何更新?包管理器可能只需要更新。 大约一个小时前有一个更新,现在安装它,我认为这是一个错误,因为该命令在 Visual Studio 2015 Express for Web 中运行良好 更新后尝试重新安装实体框架,然后再次运行您的命令。 IVsPackageInstallerServices 是包管理器用来在本地安装包的工具,因此 EF 可能没有正确安装。 我试试看,谢谢斯蒂芬! 没问题,希望能成功。 【参考方案1】:

原来这是包管理控制台的一个错误,现已修复!

【讨论】:

您是否必须安装更新才能修复它?我在 VS105u1 中遇到同样的错误 是的,安装了更新,之后一切正常【参考方案2】:

如果您的解决方案中有一个 .nuget 文件夹,请检查那里的 nuget.exe 版本...在我的情况下,该更新是修复程序。

【讨论】:

以上是关于无法使用 Entity Framework 和 Visual Studio 2015 添加迁移的主要内容,如果未能解决你的问题,请参考以下文章

我无法使用 Entity Framework Core 和 Ajax 将数据存储到数据库中

Nuget PM 中无法识别 Entity Framework Core 命令

Entity Framework Core 2.1 无法更新具有关系的实体

Entity Framework 6 将存储过程添加到数据模型后无法构建

C#console app与DB first Entity Framework和AutoMapper,无法将Model转换为DbModel

无法使用 Entity Framework 5 在 SQLite 数据库中插入记录