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

标签 c# entity-framework entity-framework-6 database-migration entity-framework-migrations

我已经使用 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。

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

谢谢!

最佳答案

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

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

我有一个临时修复,将以下绑定(bind)重定向添加到 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>

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

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

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

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

关于c# - 尝试在 Visual Studio v15.8.4 中的 Entity Framework v6.2.0 中迁移数据库时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52314359/

相关文章:

c# - 使用 Entity Framework 6.4 进行延迟加载时如何异步调用关系?

c# - 导航属性在插入后返回 null

c# - 如何将 Entity Framework 用于具有多个客户端的应用程序

c# - Unity Bootstrapper (Unity.Mvc), Unity 3, MVC 5, EF6 接收错误 : Parameterless Public Constructor on Controller

c# - EF反向POCO生成器: Failed to load provider EntityClient

c# - 在 C# 中转义字符串中的字符的最快方法

c# - 在远程服务器中创建文件夹时出错

c# - DataAdapter 更新未正确将数据插入 SQL Server?

C#、java DataInputStream.readFully() 等效项

.net - LINQ to Entities 和 String.StartsWith 的问题