使用EF5从4.5降级到4.0后无法加载文件或程序集“EntityFramework”

时间:2013-12-01 22:39:43

标签: asp.net .net entity-framework

我最近使用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>

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:9)

由于另一个主题:could-not-load-file-or-assembly-entityframework-after-downgrading-ef-5-0-0-0

,我终于解决了这个问题

尽管我使用Package Manager Console安装了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>

答案 1 :(得分:0)

使用Nuget卸载EF,然后重新添加,清理并重建项目。