C# 如何调试实体框架迁移LoaderException?

C# 如何调试实体框架迁移LoaderException?,c#,entity-framework,powershell,entity-framework-migrations,C#,Entity Framework,Powershell,Entity Framework Migrations,我正试图在另一台机器上运行的EntityFramework5项目中运行更新数据库 我下载了代码,重建了项目,当我运行updatedatabase时,我得到如下错误: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. at Sy

我正试图在另一台机器上运行的EntityFramework5项目中运行
更新数据库

我下载了代码,重建了项目,当我运行
updatedatabase
时,我得到如下错误:

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeModule.GetTypes()
   at System.Reflection.Assembly.GetTypes()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindType[TBase](String typeName, Func`2 filter, Func`2 noType, Func`3 multipleTypes, Func`3 noTypeWithName, Func`3 multipleTypesWithName)
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindConfiguration()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
我假设发生此错误是因为我的环境中缺少某个程序集,或者可能是因为GAC中发现了不兼容的程序集。但是如果不知道wich类型无法加载,我就不知道了,所以调试是一个非常困难的问题

如何找出加载失败的类型或程序集?消息说要“检索LoaderExceptions属性以获取更多信息”,但是如果迁移是在PackageManager控制台中运行的,而不是在我自己的代码中运行的,我该怎么做呢


在控制台中键入
$Error[0]。Exception
会显示异常消息,但如何列出其其他属性?

我不确定这是否对您有帮助,但只是提出了一个想法

您可以尝试从代码中运行迁移,这样您可能会得到更好的异常处理。e、 g

您还可以通过配置来实现这一点,甚至可以通过w/o重新编译(用于生产)来实现这一点——如果您启用了日志记录/跟踪,您可能会从中得到一些错误

    <contexts>
        <context type="YourNS.YourContext, YourAssembly">
            <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[YourNS.YourContext, YourAssembly], [YourNS.YourConfiguration, YourAssembly]], EntityFramework" />
        </context>
    </contexts>

</entityFramework>


是否尝试将异常强制转换为System.Reflection.ReflectionTypeLoadException并访问LoaderExceptions属性?@user1908061否,我应该在哪里执行此操作?在PowerShell命令行中?在类文件中?怎么用?这可能就是答案,在这种情况下,如果您将其作为答案发布,并提供更多详细信息,我将不胜感激。我将在控制台中键入它,就像您在
$Error[0]中所做的那样。异常
,但是这次将异常强制转换为正确的类型,并访问
LoaderExceptions
属性以获取有关确切问题的更多信息。但我很高兴你已经解决了这个问题。:-)听起来不错,但我做不到这一点,要么是因为找不到正确的语法,要么是因为出现的异常不是原始异常,也没有InnerException。我创建了一个新的控制台应用程序项目,引用了我的迁移项目并包含了您发布的代码。当它运行时,我能够看到异常并找出哪个程序集未能加载。谢谢。我不知道它会起作用,但很高兴它起作用了:)不客气@Fernandocorriea
    <contexts>
        <context type="YourNS.YourContext, YourAssembly">
            <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[YourNS.YourContext, YourAssembly], [YourNS.YourConfiguration, YourAssembly]], EntityFramework" />
        </context>
    </contexts>

</entityFramework>