且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在程序集EF6中找不到上下文类型

更新时间:2023-11-17 18:52:46

我已经解决了这个问题.

Soooo I've managed to solve the problem.

为了进行调查,我下载了Entity Framework的源代码并检查了异常堆栈中提到的所有方法.在方法System.Data.Entity.Utilities.TypeFinder.FindType中,我找到了这样的指令:

For investigation I've downloaded sources of Entity Framework and checked all methods mentioned in exception stack. In the method System.Data.Entity.Utilities.TypeFinder.FindType I've found such instruction:

var types = _assembly.GetAccessibleTypes()
                                 .Where(t => baseType.IsAssignableFrom(t));

将我带到其中找到此内容的GetAccessibleTypes()方法:

Which lead me to GetAccessibleTypes() method where I found this:

return assembly.DefinedTypes.Select(t => t.AsType());

具有DefinedTypes是System.Reflection.Assembly类的属性,我试图通过将程序集加载到Powershell中并获取此属性来手动获取此属性:

Having DefinedTypes is a property of System.Reflection.Assembly class I've tried to get this property manually by loading my assembly in Powershell and getting this property:

$a = [System.Reflection.Assembly]::LoadFrom("P:\nopCommerce\Presentation\Nop.Web\Plugins\Payments.Deposit\Nop.Plugin.Payments.Deposit.dll")
$a.DefinedTypes

结果是空白,因为根本没有类型.

The result was emptyness as there were no types at all.

所以我试图以其他方式获取类型:

So I tried to get types in a different way:

$a.GetTypes()

结果是错误:

使用"0"参数调用"GetTypes"的异常:无法加载一个 或更多要求的类型.检索LoaderExceptions属性 以获得更多信息."

Exception calling "GetTypes" with "0" argument(s): "Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."

当我检查LoaderException属性时,发现以下内容:

When I checked LoaderException property I've found following:

$Error[0].Exception.InnerException.LoaderExceptions

无法加载文件或程序集'Autofac,版本= 3.5.0.0, 文化=中性,PublicKeyToken = 17863af14b0044da'或其中之一 依赖关系.该系统找不到指定的文件.不能 加载文件或程序集'Nop.Services,版本= 3.8.0.0,文化=中性, PublicKeyToken = null"或其依赖项之一.系统无法 查找指定的文件.无法加载文件或程序集 'System.Web.Mvc,版本= 5.2.3.0,文化=中性, PublicKeyToken = 31bf3856ad364e35'或其依赖项之一.这 系统找不到指定的文件.无法加载文件或程序集 'System.Web.Mvc,版本= 5.2.3.0,区域性=中性, PublicKeyToken = 31bf3856ad364e35'或其依赖项之一.这 系统找不到指定的文件.无法加载文件或程序集 'System.Web.Mvc,版本= 5.2.3.0,文化=中性, PublicKeyToken = 31bf3856ad364e35'或其依赖项之一.这 系统找不到指定的文件.无法加载文件或程序集 'System.Web.Mvc,版本= 5.2.3.0,区域性=中性, PublicKeyToken = 31bf3856ad364e35'或其依赖项之一.这 系统找不到指定的文件.

Could not load file or assembly 'Autofac, Version=3.5.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da' or one of its dependencies. The system cannot find the file specified. Could not load file or assembly 'Nop.Services, Version=3.8.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. Could not load file or assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. Could not load file or assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. Could not load file or assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. Could not load file or assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

.NET似乎试图加载所有程序集,我的程序集依赖于并且当然不能,因为它们不在同一个文件夹中,因为它们是共享的,位于其他位置,并且是由启动项目而不是由我的程序加载的.

It looked .NET tried to load all assemblies, my assembly depended on and of course could not as they are not in same folder because they are shared, located elsewhere and loaded by start up project, not by mine.

因此,我已将所有必需的程序集都复制到了与我相同的文件夹中,并尝试再次启用Enable-Migrations.这次执行时没有任何错误!

So I've copied all required assemblies to same folder as mine and tried to Enable-Migrations again. This time it was performed without any errors!

我问自己为什么启用迁移不将所有这些程序集本身加载,因为它考虑了启动项目.但这是个小问题.

I ask myself why wouldn't Enable-Migrations load all those assemblies itself as it takes start up project into consideration. But it's rather minor issue.