Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different EntityFramework versions in same solution

I have an old Silverlight application that uses EF5 and can't be upgraded to EF6. I have another project that uses EF6 with a different context, but I get:

Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I'm assuming this is because EF5 is already loaded (it's in the main project, don't ask me why) and it's still pointing to that dll instead of the EF6 one. How can I get this to work?

I added:

   <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <codeBase version="5.0.0.0" href="C:\Projects\project\2.1.1EF2\packages\EntityFramework.5.0.0\lib\net45\EntityFramework.dll"/>
        <codeBase version="6.0.0.0" href="C:\Projects\project\2.1.1EF2\packages\EntityFramework.6.1.0\lib\net45\EntityFramework.dll"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

to my main web.config following lgos suggestion, but now I receive:

{"[A]System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection cannot be cast to [B]System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection. Type A originates from 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\project211ef\97babe28\e7ea3fa9\assembly\dl3\01275099\70646f08_d86ecf01\EntityFramework.dll'. Type B originates from 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'Default' at location 'C:\Projects\project\2.1.1EF2\packages\EntityFramework.6.1.0\lib\net45\EntityFramework.dll'."}

It looks like it's still trying to use EF5, despite it accessing the EF6 entity section.

I fixed this by adding binding redirects. In the main web.config I redirect to the new version then in a sub web.config redirect to the old version.

like image 659
Shawn Avatar asked May 27 '14 17:05

Shawn


People also ask

Should I use EF6 or EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

How do I change EntityFramework version?

Right-click on your project in Solution Explorer and select Manage NuGet Packages... Select Updates. Select EntityFramework (make sure it is going to update it to the version you want)

What are the versions of EntityFramework?

Entity Framework 6.0, 6.1, 6.2, 6.3, and 6.4 At this time the latest version is 6.4. 4. This version can always be found on NuGet. Versions 6.0, 6.1, 6.2, and 6.3 are no longer supported.

How do I reference EntityFramework DLL?

Go to references --> Add Reference --> in the dialog, choose COM and press browse. Then go to your project which is using EF and go to the projects bin folder where the EF references are stored. Select the EntityFramework.


1 Answers

It's possible to use two different versions of assembly in the same applications by defining assembly binding in config file. I think this answer should help you.

like image 171
Igos Avatar answered Sep 27 '22 23:09

Igos