Could not load file or assembly 'Microsoft.Build.Framework'(VS 2017)

70,941

Solution 1

I believe I had the same issue as you did. I didn't save the whole error message, but my error message was

'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.'

I am using Visual Studio 2017 and was trying to do Update-Database after Add-Migration.

To resolve the issue I closed Visual Studio and re-opened it, then re-ran Update-Database again.

This may or may not resolve your issue, but I thought I'd post just in case it would help.

Solution 2

Our local build script was using an older version of nuget.exe (4.7.1.5393) to restore NuGet packages. We started getting this error after updating to Visual Studio 2019 version 16.5.0. Updating to the latest version of nuget.exe (5.4.0.6315) fixed the issue for us.

nuget.exe can be downloaded here: https://www.nuget.org/downloads.

Solution 3

The root cause of this problem comes from relative paths in the devenv.exe.config file to Microsoft.Build.Framework.dll (see xml tags).

Some Visual Studio Extensions are changing the current directory and makes relative paths invalid.

To fix it, open this file in the C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\ directory. and replace all ..\..\MSBuild\15.0\Bin\ by C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\.

Solution 4

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"
  1. 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.

Solution 5

This worked for me - appears to be a non-support issue beginning in 2020.

In the Azure Build Pipeline > NuGet tool installer step, change Version of NuGet.exe to install to a newer version, like 5.4.0. Check versions at https://dist.nuget.org/tools.json.

Issue disappeared and now builds successfully.

Share:
70,941
Admin
Author by

Admin

Updated on February 20, 2022

Comments

  • Admin
    Admin about 2 years

    When I try running the command "update-database", I get this exception:

    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'

    WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

    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.`