C# 无法加载文件或程序集 EntityFramework
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16905818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not load file or assembly EntityFramework
提问by Splendor
I've deployed an ASP.NET MVC 4 application and the home page loads fine, but when I try to access any other page (which all try to connect to a SQL database) I get this error:
我已经部署了一个 ASP.NET MVC 4 应用程序并且主页加载正常,但是当我尝试访问任何其他页面(都尝试连接到 SQL 数据库)时,我收到此错误:
Could not load file or assembly 'EntityFramework, Version=4.1.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) Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileLoadException: Could not load file or assembly 'EntityFramework, Version=4.1.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=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.IO.FileLoadException:无法加载文件或程序集“EntityFramework,版本=4.1.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)
I've checked the Web.config file and it has the following relevant entries:
我检查了 Web.config 文件,它有以下相关条目:
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
...
<compilation targetFramework="4.0" />
I've read everything I could find via Google but nothing as helped so far. I know that somehow the version of EF I built the application with is different than the version that's on the deployment machine but I could use some direction in how to correct this difference.
我已经阅读了可以通过 Google 找到的所有内容,但到目前为止没有任何帮助。我知道以某种方式我构建应用程序的 EF 版本与部署机器上的版本不同,但我可以使用一些指导来纠正这种差异。
采纳答案by Pawel
You seem to be using EF5 on .NET Framework 4 (hence the version 4.4.0.0 in your config file) but the exception you get is talking about EF 4.1 (the version is 4.1.0.0). It seems like some assemblies you are using still try to use 4.1 while the other EF5 (4.4.0.0). Make sure you reference the same assembly everywhere. So, you need to update all the references to use EF5 and rebuild the project. Btw. the entry in the config file is just to point the .NET Framework to a Type that knows how to read the config section so it is not enough to update this to make the app work against EF5
您似乎在 .NET Framework 4 上使用 EF5(因此配置文件中的版本为 4.4.0.0),但您得到的例外是谈论 EF 4.1(版本为 4.1.0.0)。似乎您正在使用的某些程序集仍然尝试使用 4.1,而其他 EF5 (4.4.0.0)。确保在任何地方都引用相同的程序集。因此,您需要更新所有引用以使用 EF5 并重建项目。顺便提一句。配置文件中的条目只是将 .NET Framework 指向一个知道如何读取配置部分的类型,因此仅更新它以使应用程序针对 EF5 工作是不够的
回答by Davide Piras
you can try the following:
您可以尝试以下操作:
in the solution explorer go to the reference node and locate EntityFrameworkreference node and then in its properties set to Falsethe property Specific Version
在解决方案资源管理器中转到参考节点并找到EntityFramework参考节点,然后在其属性中设置False为该属性Specific Version
then remove the version identifier from your web.config, replace:
然后从您的 中删除版本标识符web.config,替换:
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
with simply:
简单地:
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework" requirePermission="false" />
in this way the error related to mismatching versions should b solved.
这样,与版本不匹配相关的错误应该可以解决。
still like other said in the comments, it is good if you get all your references from NuGet and check-in everything in your source control system.
仍然像评论中的其他人所说的那样,如果您从 NuGet 获取所有引用并签入源代码控制系统中的所有内容,那就太好了。
this approach has worked for me many times for many assembles and does not require any more changes in the web.config when you upgrade to a newer version of the EF later on.
这种方法在我的许多组装中对我有用过很多次,并且当您稍后升级到较新版本的 EF 时,不需要对 web.config 进行任何更改。
回答by Shoe
I encountered this while trying to debug a project locally on IIS Express.
我在尝试在 IIS Express 上本地调试项目时遇到了这个问题。
I manually unloaded the site by opening the the IIS Express options and selecting Stop Site. Then I was able to proceed normally.
我通过打开 IIS Express 选项并选择Stop Site. 然后我就可以正常进行了。
回答by Jay Dawkins
Before going through the fun of updating all the references, try restarting Visual Studio. This resolved the issue for me.
在体验更新所有引用的乐趣之前,请尝试重新启动 Visual Studio。这为我解决了这个问题。
回答by alex
In my case it was a little bit different. The DLLs were completly missing in the GlobalAssemblyCache and were not deployed by VS. I had to deploy the EntityFramework.dll and EntityFramework.SqlServer.dll to GAC, so a GAC Deployment with the following Powershell script worked:
就我而言,情况有点不同。GlobalAssemblyCache 中完全缺少 DLL,并且 VS 未部署这些 DLL。我必须将 EntityFramework.dll 和 EntityFramework.SqlServer.dll 部署到 GAC,因此使用以下 Powershell 脚本的 GAC 部署工作:
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("C:\Path_To_DLL\EntityFramework.dll")
$publish.GacInstall("C:\Path_To_DLL\EntityFramework.SqlServer.dll")

