Different EntityFramework versions in same solution

这一生的挚爱 提交于 2019-12-08 19:47:58

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/23895800/different-entityframework-versions-in-same-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!