无法加载文件或程序集'EntityFramework'或其某个依赖项

8

我的ASP.NET MVC3网络应用程序一直运行良好。但是当我重新启动电脑并尝试在MS VS 2010中更新应用程序时,它在运行应用程序期间显示错误:

Could not load file or assembly 'EntityFramework' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

描述:

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. 

异常细节: System.IO.FileLoadException: 无法加载文件或程序集“EntityFramework”或其某一个依赖项。参数不正确。(HRESULT 异常信息: 0x80070057 (E_INVALIDARG))

错误源:

当前网页请求执行期间发生未处理的异常,可以使用以下异常堆栈跟踪信息确定异常的原因和位置。

程序集加载跟踪:以下信息有助于确定为什么无法加载“EntityFramework”程序集。

警告: 程序集绑定记录已关闭。 若要启用程序集绑定失败记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) 设置为 1。 注意:启用程序集绑定失败记录会带来一定的性能损失。 若要关闭此功能,请删除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。

堆栈跟踪:

[FileLoadException: Could not load file or assembly 'EntityFramework' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))]

[FileLoadException: Could not load file or assembly 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))]
System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +39
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks) +132
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +144
System.Reflection.Assembly.Load(String assemblyString) +28
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +46

[ConfigurationErrorsException: Could not load file or assembly 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))]
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +618
System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +209
System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +130
System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +178
System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +94
System.Web.Compilation.BuildManager.CallPreStartInitMethods() +332
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +591

[HttpException (0x80004005): Could not load file or assembly 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +8946484
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +258

我清除了所有Windows临时文件和ASP.NET临时文件,然后再次运行应用程序。但情况并没有改善。请您帮忙解决一下。谢谢。

5个回答

14
如果其他答案对您不起作用(包括SO上关于此问题的其他类似问题的答案),请尝试以下解决方法,这对我有效(根据http://forums.devart.com/viewtopic.php?f=3&t=26327):
将以下XML添加到应用程序的*.config文件中以解决问题:
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" />
      <bindingRedirect oldVersion="4.4.0.0" newVersion="5.0.0.0" />
    </dependentAssembly>
    </assemblyBinding>
  </runtime>

14

只需重新添加Entity Framework Tools-Library包管理器->包管理器控制台

输入:

Install-Package EntityFramework

它将还原(您正在运行v4.4,这是Visual Studio 2010中的v5版本)

如果您想要“清理”,可以尝试先卸载。 即使失败也没有任何损害。

Uninstall-Package EntityFramework

2
由于依赖关系,我无法将其删除,但重新安装有所帮助:Update-Package -reinstall EntityFramework。 - Juha Palomäki

8

确保所有项目都针对相同的.NET Framework。我有一个.NET 4.5项目,它引用了一个使用4.0的类库,我遇到了这个问题,一旦我将其更改为4.5,它就可以正常工作,并且我可以使用NuGet包管理器控制台安装EF。


3
我遇到了这个错误。原因比其他答案中的要简单:我有两个项目引用了不同版本的Entity Framework。其中一个来自包含最新版本的NuGet包,而另一个来自ASP.NET MVC 4模板。
我的解决方法是从MVC 4项目中删除对旧版EF(5)的引用,并使用NuGet安装最新版。之后一切都正常运行。

2

我曾遇到类似的问题,解决方法是手动删除解决方案中的包文件夹以及packages.config文件中对该包的引用,然后使用“包管理器控制台”重新安装即可。


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