dotnet / ef6

This is the codebase for Entity Framework 6 (previously maintained at https://entityframework.codeplex.com). Entity Framework Core is maintained at https://github.com/dotnet/efcore.
https://docs.microsoft.com/ef/ef6
MIT License
1.43k stars 545 forks source link

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

Closed CZEMacLeod closed 5 years ago

CZEMacLeod commented 7 years ago

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.

vlm--- commented 6 years ago

This issue started for us after we migrated our projects to PackageReference. In other solution where we have some projects with EF not yet migrated to PackageReference it somehow works, error is still displayed at the end but all EF commands do their job. Unfortunately I can't post any logs from this environment.

LeonardoX77 commented 6 years ago

including mentioned MsBuild references by @avonwyss and calling MSBuildLocator.RegisterDefaults() results now in this error: System.IO.FileLoadException: Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401)

johanjvr commented 5 years ago

I've had the same issue just changed the devenv.exe.config file to use the full path instead of the relative path.

<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\Enterprise\MSBuild\15.0\Bin\Microsoft.Build.Framework.dll" /> </dependentAssembly>

LeonardoX77 commented 5 years ago

@johanjvr I tried your solution but it didn't work for me :( Restarting VS2017 works sometimes and it seems to be currently the only workaround?. This is very annoying... @ajcvickers please consider giving this issue a higher priority. Thanks Regards

johanjvr commented 5 years ago

@johanjvr I tried your solution but it didn't work for me :( Restarting VS2017 works sometimes and it seems to be currently the only workaround?. This is very annoying... @ajcvickers please consider giving this issue a higher priority. Thanks Regards

Hi @LeonardoX77, I've found the problem eventually it was caused by a dynamic filter which I've added to the Constructor:

SetFilterScopedParameterValue

I was using the variable which was still null at the time. This I found by enabling the Fusion logging maybe try enabling it and see if you can find the reason why it's giving you this error.

Johan

LeonardoX77 commented 5 years ago

Hi @johanjvr Thanks for your reply. Sorry, could you please provide more details about what constructor and variable are you talking about and if I can follow this guide to enable Fusion logging? Thanks Leo

gabbsmo commented 5 years ago

My project is not using PackageReference and I still have the issue so this does not appear to be a factor.

rstarkov commented 5 years ago

In my case this error occurs only when running the Seed method, and more specifically, only when the overridden SaveChanges method attempts to access System.Web.HttpContext.Current for audit logging purposes - which normally simply returns null when called via Update-Database.

bricelam commented 5 years ago

This may be fixed in 6.3.0-preview8--this area of the code was changed significantly while adding support for .NET Core

Spongman commented 4 years ago

I'm running 16.4.1 and I'm seeing this same issue during Update-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'

not fixed. worse, this is a regression, this is when re-running migrations that used to work.

LeonardoX77 commented 4 years ago

Also in 16.4.2

habib-developer commented 4 years ago

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.

This works for me