出现错误:无法加载文件或程序集“EntityFramework, Version=6.0.0.0”。

17

我正在使用 EF 进行工作。 我试图执行这一行

public ActionResult Edit(string id)
{           
     return View(obj.FindSemesterById(id));
}

我在我的项目上安装了EF 5版本。

但是我遇到了这个错误:

无法加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或其某一个依赖项。所定位的程序集清单定义与程序集引用不匹配。(HRESULT 的异常来自:"0x80131040")

我的web.config文件:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="EducationDBEntities" connectionString="metadata=res://*/EducationModel.csdl|res://*/EducationModel.ssdl|res://*/EducationModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=EducationDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>

    </assemblyBinding>

  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

2
为什么不安装EF 6版本,因为它显然正在寻找那个版本? - Michael Edenfield
1
你是否遵循这些指示?http://www.nuget.org/packages/EntityFramework/ - Michael Edenfield
@Michael Edenfield:我刚在NuGet包中搜索并安装了它。 - Ehsan Akbar
我应该如何编写这个命令? - Ehsan Akbar
@user3446201 我已经编辑了我的答案,因为在你的 web.config 文件中引用了 EF 版本 5.0.0.0。 - NullReferenceException
显示剩余9条评论
8个回答

9

从评论部分来看,您似乎无法从公共NuGet源安装最新版本的EF,因为您的计算机无法直接访问互联网并且无法解析www.nuget.org域名。通常情况下,如果您在Internet设置中配置了代理,Visual Studio将在从公共存储库安装NuGet时使用此代理。

因此,一旦您在项目中安装了最新的EF 6.1.0包,错误就会消失。目前,您似乎正在使用某些旧版本的软件包,并且您的解决方案中有需要v6的项目。


我将它改为EF6,但是另一个错误出现了: 无法加载文件或程序集 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 或其依赖项之一。找到的程序集清单定义与程序集引用不匹配。(HRESULT 异常:0x80131040) - Ehsan Akbar
2
看起来你在解决方案中引用了一些需要 EF 5 的项目。这可能是因为你正在使用某些第三方库。首先分析你正在使用的包,并寻找与 EF6 编译和兼容的版本。你应该安装一致的版本。你不能只是将任意 NuGet 引入你的解决方案,然后期望它能正常工作。你还应该了解这些 NuGets 在做什么,以及它们有哪些依赖关系,以避免这种冲突。 - Darin Dimitrov

9

首先,检查您正在使用的EF版本。您可以从NuGet Packet Manager更新EF版本。

转到您的项目解决方案->“管理解决方案的NuGet包”,在“Entity Framework”上点击“管理”。

还要检查您的app.config。可能会引用错误的版本号。

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

它引用的是EF版本5.0.0.0。


不要像Darin Dimitrov说的那样,首先尝试管理Nuget包。然后你的问题就会得到解决。 - NullReferenceException

6

打开 工具 > NuGet程序包管理器 > 程序包管理器控制台

然后运行

PM> install-package entityframework -version 6.0.0.0

您可以使用以下命令将其更新为最新版本:

或者

PM> Update-Package entityframework 

1
使用Nuget卸载EF,然后再次添加它,清理并重新构建您的项目,这会起到神奇的作用。在我的情况下,“卸载”部分非常关键,我进行了许多重新安装和更新,但都没有效果。

0

我的解决方案中有四个项目。这是一个不同的计算机,不同于我最初创建解决方案的计算机,所以我打开了工具,然后NuGet包管理器,点击“管理解决方案的NuGet包”,找到Entity Framework并单击它一次。然后点击出现的“管理”按钮。

NuGet提供将EF安装到解决方案中的一个项目中,所以我这样做了。那时我开始遇到这个错误。

回到NuGet并勾选将EF安装到解决方案中的所有四个项目中,问题得到了解决。


0

我遇到了和你提到的相似的问题和同样的错误。

我的解决方法有所不同。我注意到EntityFramework.dll被添加到了Solution Explorer下的packages文件夹中。 因此,我从那个文件夹中复制了EntityFramework.dll到Solution Explorer下的bin文件夹中,然后它就可以工作了。 我知道这并不是你的答案,但对其他人可能会有帮助,所以我加上了它。


0
今天我遇到了同样的问题。我的解决方案中有3个项目,其中两个使用EF 6.0.0.0,另一个使用5.0.0.0。当我发现这个问题时,我升级了那个项目的版本。错误消失了。
注意:我只是进入每个项目的引用文件夹,并查看EntityFramework的属性,以找出Entity的版本。

这只是重复现有的答案。 - TylerH

0

如果您的项目名称与您引用的NuGet包发生冲突,就会出现这种情况。


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