efcore: Could not load file or assembly System.ComponentModel.Annotations, Version=4.2.0.0

Hi

the past few days I tried to get out of “DLL hell 2.0” aka nuget packages 😃 I setup my whole solution on VS in a brand new directory, created each project as a new one and only tool the sources over. Doing this I also updated alle the nuget packages to the latest version. Running the app it hits an exception when doing “Database.Migrate()”

I tried every workaround I found in the web. Adding binding redirect in the csproj, manually settings the version numbers in the app.config, downgrading everything to the latest stable (hitting another exception (System.Interactive.Async Version=3.0.3000.0 - also well documented and for me unresolvable)

My current state is nuget packages as described below and hitting the Annotation version conflict.

I hope one of the knowing developers can hint me how to fix that issue. (am I the only one? if not, why do the devs getting the same exception not document hoe to work around while not the nuget “fire and forget” mentality works?)

so any advice is welcome 😃

Exception message:
Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies

Further technical details

EF Core version: 2.2.0-preview1-35029 Database Provider: SQLite provider Operating system: Win10 IDE: VS 15.8.3

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 108 (50 by maintainers)

Most upvoted comments

In some form this problem is already there for more then two years. In my experience it only shows up in rather complex solutions were both .Net Core (EF Core) and traditional .Net Frameworks (both 4.6.1 and 4.7.2) are used. Since the introduction of .Net Core 2.1 the problem can nearly always be solved by manually editing your config file in one or more projects (app.config in my case). Include the lines below in the runtime section.

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
  <dependentAssembly>
     <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
     <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.0.0.0" />
  </dependentAssembly>
</assemblyBinding>

In some form this problem is already there for more then two years. In my experience it only shows up in rather complex solutions were both .Net Core (EF Core) and traditional .Net Frameworks (both 4.6.1 and 4.7.2) are used. Since the introduction of .Net Core 2.1 the problem can nearly always be solved by manually editing your config file in one or more projects (app.config in my case). Include the lines below in the runtime section.

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
  <dependentAssembly>
     <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
     <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.0.0.0" />
  </dependentAssembly>
</assemblyBinding>

LOVE YOU ^ MS - Can this not simply be fixed already this kind of stuff is really confusing!!!

JRBonnema’s code worked for me. I had to use newVersion="4.0.0.0" even though I had 4.5 installed.

@neil-rubens: THANK YOU VERY MUCH!!! this solves the issue. Unbelievable that the cause to switch to the new VS project files (automatically resolving version “conflicts” with assemblies) is the reason for those deep problems. And second unbelievable that MS cannot solve this internally. There has to be a customer to get deep into the resolving issues and hand out a solution. In my case, due to the fact that it only happens framework core internally while updating the database, it is enough to redirect the resolver when starting the program, and stopping it (by unregistering the event) when main form and dlls are loaded. while the program is running everything seems to be ok. And if some day the exception occurs while executing any db operation, I can prevent this by not unregistering the event.

Despite from having a solution that works for me and for now, it would be a more reliable solution if this is fixed Microsoft internally…

None of the above solutions worked for me so for a .NET 4.7.2 console application which is going to be run as a Windows Service. It’s complaining about not finding the System.ComponentModel.Annotations 4.2.0, but there doesn’t even seem to be such a version, especially not on nuget. Almost 2021 and still in DLL Hell as it seems.

Same issue:

Error CS1061: 'DbContextOptionsBuilder<MemoryContext>' does not contain a definition for 'UseInMemoryDatabase' and no accessible extension method 'UseInMemoryDatabase' accepting a first argument of type 'DbContextOptionsBuilder<MemoryContext>' could be found (are you missing a using directive or an assembly reference?)
Process 'msbuild.exe' exited with code '1'.

It is working locally, but VSTS throws the exception above.

This did not work for me - may be because I do not know which *.config files are affected. There are many relations so I guess there should be entries in the config file, where I do not think they should be needed. The whole solution is 86 projects. Adding the lines to the config files where database (ef core with .Net Core) is used, it still throws the exception, Adding them to config files of “higher” projects (that make use of projects with database) still doesn’t to the trick. A solution like yours would again speedup the application start by far - the assembly resolver is little slow…

using assembly redirection worked for me: https://stackoverflow.com/a/50776946/2705777

Experienced same issue in asp.net solution in integration tests project while porting EF->EF Core. Fixed with adding following binding redirect to IntesrationTests/app.config. First adding following binding redirect to each and every app.config/web.config fixed that error, than narrowed down to the integration tests’ app.config itself

<configuration>
	<runtime>
		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
			<dependentAssembly>
				<assemblyIdentity name="System.ComponentModel.Annotations" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
				<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
			</dependentAssembly>
		</assemblyBinding>
	</runtime>
</configuration>

@joperezr Okay, thanks. I’ll leave this issue open until we have somewhere to refer people.

Hi all!

I made the simple solution for demo of this error. In pdf-file step by step instruction for creating solution. In zip-file - archive of ready solution for testing. Sorry, “packages” folder removed from archieve (zip-file can not more than 10Mb)

AppTest8.Data — without packages folder.zip How to get this error.pdf