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?
Thanks!
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 3.0.0.0 of the System.Management.Automation assembly is loaded in the package management console, and EntityFramework.PowerShell.Utility.dll is build against version 1.0.0.0.
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
<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>
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