question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add-Migration and Update-Database fail because of attempt to load assembly Microsoft.Build.Framework

See original GitHub issue

With 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:closed
  • Created 6 years ago
  • Reactions:12
  • Comments:62 (9 by maintainers)

github_iconTop GitHub Comments

28reactions
linguanostracommented, Dec 22, 2017

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.

11reactions
tompazourekcommented, Dec 12, 2017

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:

<codeBase version="15.1.0.0" href="..\..\MSBuild\15.0\Bin\Microsoft.Build.Framework.dll" />

With this one (I’m using VS 2017 Community 15.5.0):

<codeBase version="15.1.0.0" href="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Build.Framework.dll" />

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

add-migration causing a "Could not load assembly" error
You get the error: Could not load assembly 'DataProject'. Ensure it is referenced by the startup project 'ProgramProject'.
Read more >
add-migration gives error "Could not load file or assembly ' ...
Whenever we use the package manager console like "add-migration" or "update-database" we get this error: System.IO.FileNotFoundException: Could ...
Read more >
Error while Add-Migration "Initialize" in .NET 6 Entity ...
The issue was, my startup project was chosen wrong. Fixing: I fixed it by setting the web project as a startup. You can...
Read more >
Could not load file or assembly class library " ...
Open the project file in Solution Explorer, is there an entry as shown below (using a .NET Core project), in this case I'm...
Read more >
Cant Update dataBase after Adding a new Migration In EF ...
To undo this action, use Remove-Migration. PM> Update-Database Build started... Build succeeded. fail: Microsoft.EntityFrameworkCore.Database.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found