尝试在 Entity Framework v6.2.0 和 Visual Studio v15.8.4 中迁移数据库时出现异常

Exception when trying to migrate database in Entity Framework v6.2.0 in Visual Studio v15.8.4

我已经使用 Entity Framework 设置了一个数据库,首先是代码,每次我尝试添加迁移或更新数据库时,我都会得到以下 error message:

PM> update-database
Exception calling "CreateInstance" with "7" argument(s): "Constructor on type 'System.Data.Entity.Migrations.Utilities.DomainDispatcher' not found."
At D:\WTS Projects\ModelCalibration\packages\EntityFramework.6.2.0\tools\EntityFramework.psm1:783 char:5
+     $dispatcher = $utilityAssembly.CreateInstance(
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : MissingMethodException

System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.Entity.Migrations.MigrationsDomainCommand.WriteLine(String message)
at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Object reference not set to an instance of an object.

我已将该项目复制到另一台具有相同版本 Visual Studio、Entity Framework 和 Windows 的计算机,并且我能够毫无问题地迁移数据库。我还在两台机器上的这些 docs 上下载了示例项目,并在这台机器上遇到了同样的异常,但另一台机器也没有问题,所以我相信这与我的代码无关。

我能找到的唯一解决方案是在此线程 here 上,解决方案是重新安装 windows,我不太愿意这样做。

我也尝试了其他类似 SO 帖子中所说的一切,例如 Exception raised when I'm trying enable migrations in EF 4.3.1 ,就是要,重新安装 Entity Framework,修复 Visual Studio,等等...

我正在使用 Enitiy Framework v6.2.0、Visual Studio v15.8.4、.NET Framework 4.6.1 和 Windows 10.

有没有人在and/or之前见过这个问题知道任何可能的解决方案?

谢谢!

有时只需清理并重建您的解决方案就可以了

好的,我找到了一个有效的解决方案,感谢@rho24 thread!我还将在这里为有问题的任何人重新发布解决方案:

问题是在我的机器上 System.Management.Automation 程序集的版本 3.0.0.0 加载到包管理控制台中,EntityFramework.PowerShell.Utility.dll 是针对版本 1.0.0.0 构建的。

我临时修复了将以下绑定重定向添加到 C:\Users\USERNAME\AppData\Local\Microsoft\VisualStudio.0_313c23f6\devenv.exe.config

<dependentAssembly>
    <assemblyIdentity name="System.Management.Automation" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
    <publisherPolicy apply="no" />
</dependentAssembly>

请注意,每次更新 VS 时都需要应用此修复程序。

我发现这个问题与这个问题相同 PowerShell/PowerShell#6189

我也有,但在最近的 VS 更新中已修复。希望他们应用的修复对您来说也很容易!

再次感谢@rho24 的解决方案! :D