无法加载文件或程序集tools\EntityFramework.PowerShell.Utility.dll。

25

尝试从程序包管理器控制台安装实体框架6.1.3时,我得到了以下输出。

Install-Package EntityFramework 
Attempting to gather dependency information for package 'EntityFramework.6.1.3' with respect to project 'OnlineShop', targeting '.NETFramework,Version=v4.6.1'
Attempting to resolve dependencies for package 'EntityFramework.6.1.3' with DependencyBehavior 'Lowest'
Resolving actions to install package 'EntityFramework.6.1.3'
Resolved actions to install package 'EntityFramework.6.1.3'
Adding package 'EntityFramework.6.1.3' to folder 'D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\OnlineShop\packages'
Added package 'EntityFramework.6.1.3' to folder 'D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\OnlineShop\packages'
Added package 'EntityFramework.6.1.3' to 'packages.config'
Executing script file 'D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\OnlineShop\packages\EntityFramework.6.1.3\tools\install.ps1'
Exception calling "LoadFrom" with "1" argument(s): "Could not load file or asse
mbly 'file:///D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\OnlineShop
\packages\EntityFramework.6.1.3\tools\EntityFramework.PowerShell.Utility.dll' o
r one of its dependencies. The system cannot find the file specified."
At D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\OnlineShop\packages\E
ntityFramework.6.1.3\tools\EntityFramework.psm1:780 char:5
+     $utilityAssembly = [System.Reflection.Assembly]::LoadFrom((Join-Path $Too
lsP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException

You cannot call a method on a null-valued expression.
At D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\OnlineShop\packages\E
ntityFramework.6.1.3\tools\EntityFramework.psm1:781 char:5
+     $dispatcher = $utilityAssembly.CreateInstance(
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling "CreateInstanceFrom" with "8" argument(s): "Could not load fi
le or assembly 'file:///D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\
OnlineShop\packages\EntityFramework.6.1.3\tools\EntityFramework.PowerShell.dll'
 or one of its dependencies. The system cannot find the file specified."
At D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\OnlineShop\packages\E
ntityFramework.6.1.3\tools\EntityFramework.psm1:809 char:5
+     $domain.CreateInstanceFrom(
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException

Exception calling "LoadFrom" with "1" argument(s): "Could not load file or asse
mbly 'file:///D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\OnlineShop
\packages\EntityFramework.6.1.3\tools\EntityFramework.PowerShell.Utility.dll' o
r one of its dependencies. The system cannot find the file specified."
At D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\OnlineShop\packages\E
ntityFramework.6.1.3\tools\EntityFramework.psm1:780 char:5
+     $utilityAssembly = [System.Reflection.Assembly]::LoadFrom((Join-Path $Too
lsP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException

You cannot call a method on a null-valued expression.
At D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\OnlineShop\packages\E
ntityFramework.6.1.3\tools\EntityFramework.psm1:781 char:5
+     $dispatcher = $utilityAssembly.CreateInstance(
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling "CreateInstanceFrom" with "8" argument(s): "Could not load fi
le or assembly 'file:///D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\
OnlineShop\packages\EntityFramework.6.1.3\tools\EntityFramework.PowerShell.dll'
 or one of its dependencies. The system cannot find the file specified."
At D:\Leva\Work\Informatika\CSharp\My OPENSOURCE projects\OnlineShop\packages\E
ntityFramework.6.1.3\tools\EntityFramework.psm1:809 char:5
+     $domain.CreateInstanceFrom(
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException


Type 'get-help EntityFramework' to see all available Entity Framework commands.
Successfully installed 'EntityFramework 6.1.3' to OnlineShop

正如您所看到的,存在异常情况。但毕竟,尽管存在异常,Entity Framework 6.1.3已成功添加到项目中。此外,当我添加对System.Data.Entity的引用并尝试在代码中使用它时,无法连接到System.Data.Entity命名空间并使用该命名空间的库。这是否与上述异常有关,并且如何修复?


尝试以管理员模式打开Visual Studio,然后再试一次。 - ssanga
13个回答

73

使用以下命令:

EntityFramework6\Add-Migration

微软已更改cmdmidlet的名称


答案似乎与此问题无关。 - Deb
有任何来源吗?虽然它解决了问题,但文档中没有提到。 - Drag and Drop
3
谢谢,对我有用。 执行以下命令:PM> EntityFramework6\Add-Migration -ConfigurationTypeName ProjectNAME.DataContexts.DbMigrations.Configuration "Abc123456" - GinCanhViet
这可能是最好的答案,因为在使用VS 2022将EF降级到较旧版本时,会在生成/执行迁移时引发ArgumentNullException异常。 - undefined

11

我通过将Entity Framework 6.1.3降级到Entity Framework 6.1.2来解决了这个问题。可能是最新版本的一个bug。


2
这对我来说可行,使用的是 EntityFramework V6.4.0,甚至可以向下兼容到 V6.2.0。 - Zac Faragher

6

我在.NET Framework 4.5项目中的Visual Studio 2019 (v16.5.4)使用EntityFramework 6.4.0时遇到了同样的问题。

PM> Enable-Migrations
Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\Users\Administrator\source\repos\InfrastructureManager\packages\EntityFramework.6.4.0\tools\EntityFramework.PowerShell.Utility.dll' or one of its dependencies. The system cannot find the file specified."
At C:\Users\Administrator\source\repos\InfrastructureManager\packages\EntityFramework.6.1.3\tools\EntityFramework.psm1:780 char:5
+     $utilityAssembly = [System.Reflection.Assembly]::LoadFrom((Join-P ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException
 
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\source\repos\InfrastructureManager\packages\EntityFramework.6.1.3\tools\EntityFramework.psm1:781 char:5
+     $dispatcher = $utilityAssembly.CreateInstance(
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Exception calling "CreateInstanceFrom" with "8" argument(s): "Could not load file or assembly 'file:///C:\Users\Administrator\source\repos\InfrastructureManager\packages\EntityFramework.6.4.0\tools\EntityFramework.PowerShell.dll' or one of its dependencies. The system cannot find the file specified."
At C:\Users\Administrator\source\repos\InfrastructureManager\packages\EntityFramework.6.1.3\tools\EntityFramework.psm1:809 char:5
+     $domain.CreateInstanceFrom(
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException
 

解决方案为 (包管理器控制台):
Uninstall-Package EntityFramework -Force
Install-Package EntityFramework -Version 6.1.2
Enable-Migrations

5

从你的项目中卸载 EntityFramework,然后 重启 项目并重新安装 EntityFramework。


寻求帮助的各位:重新启动VS2022(EF6.4.4与EF6.2.0出现相同问题)解决了这个问题。如果不重启,我会一直收到相同的错误提示,即使在任何地方都找不到“.....packages\EntityFramework.6.2.0”文件夹。感谢Davood和@Christian。 - Pap

5

对我来说,解决这个问题的方法是将解决方案中的所有EF项目升级到相同的EF版本。我不认为微软的产品差。我喜欢VS集成开发环境,没有什么可以与之媲美。


1
这对我有用。我将我的一些项目从6.2更改为6.4.4。但是当我将所有项目都更改后,它删除了6.2文件夹并停止混淆。这确实需要额外的清理/重建解决方案和重新启动Visual Studio。 - Kevin UI
将所有项目更新到 EF 6.4.4 对我解决了这个问题,谢谢。 - motta
我在移除旧版 EF 后不得不重新启动 Visual Studio,否则它无法工作。 - Nir
对我来说,关键是将解决方案中的所有项目升级到6.4.4版本,然后重新启动Visual Studio。 - pjaaar

2

我使用的是EF 6.4.4版本。更改为6.2.0版本解决了问题。


1
如果在程序包管理器控制台中没有选择正确的“默认项目”,您可能会遇到此错误。将其切换到您的主应用程序项目即可解决问题。
或者您可以像这样使用“-ProjectName”选项:
Add-Migration -Name“一些更改” -ProjectName MyWebProject

0

卸载-Package EntityFramework -Force 安装-Package EntityFramework -Version 6.1.2 启用-Migrations


1
请不要发布仅包含代码的答案。未来的读者会感激您解释为什么这个答案回答了问题,而不是从代码中推断出来。此外,由于这是一个旧问题,请解释它如何补充所有其他答案。 - Gert Arnold

0

通过将所有项目升级到最新的EntityFramework(6.4.4)来解决问题。


0
在我的情况下,将Dll从EF 6.4.4更改为EF 6.0.0可以解决问题。

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