Add-Migration and Update-Database fail because of attempt to load assembly Microsoft.Build.Framework
See original GitHub issueWith VS2017 15.4 or 15.5 (Preview), I regularly get a fail when adding or applying migrations due to the error
Could not load file or assembly 'Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
The result from e.g. Update-Database is
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at xxxxxx.Migrations.Configuration..ctor()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Data.Entity.Migrations.Utilities.MigrationsConfigurationFinder.FindMigrationsConfiguration(Type contextType, String configurationTypeName, Func`2 noType, Func`3 multipleTypes, Func`3 noTypeWithName, Func`3 multipleTypesWithName)
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindConfiguration()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Obviously my class library and main project do not include any references to MSBuild Framework and the update does work at runtime.
Closing VS and re-opening then running the command before doing anything else sometimes seems to resolve the issue.
I did find a proposed solution online which involved editing DevEnv.exe.config
and replacing the relative path for the codebase element with an absolute path - although this doesn’t seem to have worked in my case.
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="2.0.0.0-99.0.0.0" newVersion="15.1.0.0" />
<codeBase version="15.1.0.0" href="C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Build.Framework.dll" />
</dependentAssembly>
This could be a red-herring however, as the solution said to close VS, edit the .config and then re-open which in my case seems to resolve the issue (temporarily) anyway.
Adding that section to my web.config results in a different error Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401)
Any guidance or ideas?
N.B. this happens to me with both EF 6.1.3 and the latest 6.2.0-beta2-60920 and I am targeting net461.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:12
- Comments:62 (9 by maintainers)
I’ve found a workaround that seems to resolve the issue for good, at least on my environment running VS 2017 Professional 15.5.2 and Entity Framework 6.1.1.
Basically, install the DLL (with a few related ones) into the GAC (Global Assembly Cache) and the issue will go away.
Follow these steps:
1- Close all running instances of Visual Studio 2017
2- Launch the Visual Studio 2017 Developer Command Prompt
3- Type the following commands (replace Professional with your edition, either Enterprise or Community, or adjust the path accordingly):
gacutil /i “C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Build.Framework.dll”
gacutil /i “C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Build.dll”
gacutil /i “C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Build.Engine.dll”
gacutil /i “C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Build.Conversion.Core.dll”
gacutil /i “C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Build.Tasks.Core.dll”
gacutil /i “C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Build.Utilities.Core.dll”
4- Restart Visual Studio 2017
In essence, the GAC will (in most cases) be given priority when .NET is trying to load a DLL and the FileNotFoundException will go away as your DLL will now be resolved through the GAC.
Again, it works for me and it’s simply a workaround, it won’t solve the core issue itself but at least I don’t have to restart VS all the time when trying to work with EF migrations, and that’s good enough for me.
I had the same issue several times and always used to resolve it with a VS restart (which is annoying, because for a big solution and when using ReSharper, Task Runner, etc. it takes a long time for everything to start).
But this time, restarts were no longer working (tried to restart it like 4 times, running Clean solution, etc.) and I was desperate as I was under time pressure and couldn’t add migrations I needed… I finally tried the suggested fix and replaced this line in
devenv.exe.config
:With this one (I’m using VS 2017 Community 15.5.0):
Then I restarted VS and it started working again. When I searched for a solution on Google, it seemed that many people are having the exact same issue…
I use EF 6.2.0.