尝试在Visual Studio v15.8.4中的Entity Framework v6.2.0中迁移数据库时发生异常

Alf*_*fie 2 c# entity-framework database-migration ef-migrations entity-framework-6

我每次尝试添加迁移或更新数据库时都使用Entity Framework,代码先建立数据库,然后收到以下错误消息

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.
Run Code Online (Sandbox Code Playgroud)

我已将项目复制到具有相同版本的Visual Studio,Entity Framework和Windows的另一台计算机上,并且能够完全迁移数据库。我还在两台计算机上的这些文档上下载了示例项目,并且在这台计算机上遇到了相同的异常,但是另一台也没有问题,因此我认为这与我的代码无关。

唯一的我已经能够找到的解决方案是在这个线程在这里,这里的解决办法是重新安装它,我不那么热衷做的窗户。

我还尝试了其他类似SO文章中所说的所有内容,例如尝试在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。

有没有人以前见过这个问题和/或知道任何可能的解决方案?

谢谢!

Alf*_*fie 6

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

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

我有一个临时修复程序,将以下绑定重定向添加到 C:\Users\USERNAME\AppData\Local\Microsoft\VisualStudio\15.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>
Run Code Online (Sandbox Code Playgroud)

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

我能够弄清楚这是与这件事相同的问题PowerShell/PowerShell#6189

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

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