降级从EF4.5到EF4.0后无法加载文件或程序集'EntityFramework'

6

我最近使用Entity Framework 5开发了一个.net 4.5库。一切都很正常,直到有人告诉我必须降级到.net 4.0才能兼容Windows 2003。

我使用包管理器控制台卸载了EF 5,并收到了成功的消息,然后是这个:

Failed to generate binding redirects for 'MyProjectName'. An item with the same key has already been added.

降级和安装 Entity Framework 后出现相同的错误信息。所有内容均已编译。但在执行时,会出现以下异常:
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.

Could not load file or assembly 'EntityFramework, Version=5.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)":"EntityFramework, Version=5.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"

在 Web.Config(以及库的 App.Config)中,我有以下内容:
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
...
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089"
                      culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
        </dependentAssembly>
   </assemblyBinding>
</runtime>

非常感谢您的帮助。


如果您在项目中添加了Entity Framework的引用,请删除并重新添加引用到新的dll,然后重新构建。 - Mike Beeler
我做了,但是没有帮助。 - Francois Stock
2个回答

9
我最终通过另一个主题解决了这个问题:could-not-load-file-or-assembly-entityframework-after-downgrading-ef-5-0-0-0
尽管我使用包管理器控制台安装了EF 5软件包,但.NET 4.0的实际版本是4.4。我在配置文件中更改了值,它起作用了!
<section name="entityFramework"
    type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework,
    Version=4.4.0.0, Culture=neutral, 
    PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
...
<dependentAssembly>
    <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089"
    culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="4.4.0.0"/>
</dependentAssembly>

问题的另一个部分是,实体框架4.0是随 .NET 一起发布的,而5.0则与.NET解耦,并作为NuGet包提供。 - Mike Beeler
2
如果有人在查找,Entity Framework 5.0所需的最低.NET框架版本为.NET 4.0或4.5。 - phil soady
如果您明确要求软件包管理器安装EF 4.4,它会告诉您该版本不存在。 - Francois Stock

0
使用Nuget卸载EF,然后再次添加它,清理并重新构建您的项目。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接