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

c# database-migration ef-migrations entity-framework entity-framework-6


I have setup a database using Entity Framework, code first, every time I try to add a migration or update the database I get the following 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.

I have copied the project to another computer with the same version of Visual Studio, Entity Framework & Windows and I am able to migrate the database with no issues at all. I have also downloaded the sample project on these docs on both machines, and got the same exception on this machine but the other one again had no issues, so I believe its nothing to do with my code.

The only solution I have been able to find is on this thread here, where the solution was to reinstall windows which I am not so keen to do.

I have also tried everything said in other similar SO posts, such as Exception raised when I'm trying enable migrations in EF 4.3.1 , which was to, reinstall Entity Framework, repair Visual Studio, etc...

I am using Enitiy Framework v6.2.0, Visual Studio v15.8.4, .NET Framework 4.6.1 and Windows 10.

Has anyone seen this issue before and/or know any possible solutions?


9/25/2018 8:33:42 AM

Accepted Answer

Okay I found a solution that works, thanks to @rho24 on this thread! I will also repost the solution here for anyone that has the issue:

The problem is that on my machine is that version of the System.Management.Automation assembly is loaded in the package management console, and EntityFramework.PowerShell.Utility.dll is build against version

I have a temporary fix of adding the following binding redirect to C:\Users\USERNAME\AppData\Local\Microsoft\VisualStudio\15.0_313c23f6\devenv.exe.config

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

Note, the fix needs to be applied everytime you update VS.

I was able to figure this out as its the same issue as this one PowerShell/PowerShell#6189.

Which i was also having, but was fixed in a recent VS update. Hopefully the fix they applied is easy for you too!

Again credit to @rho24 for the solution! :D

9/20/2018 9:04:25 AM

Popular Answer

It sometimes work to just clean and rebuild your solution

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow