2013-04-02 26 views
1

我試圖在其他機器上運行的Entity Framework 5項目中運行update-database如何調試實體框架遷移LoaderExceptions?

我下載的代碼,重建項目,當我運行update-database我得到一個錯誤這樣的:

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中找到不兼容的組件。但是如果知道這種類型的加載失敗,我就處於黑暗中,因此調試起來非常困難。

我怎樣才能找出哪種類型或程序集加載失敗?該消息表示「檢索LoaderExceptions屬性以獲取更多信息」,但如何在包管理器控制檯中運行遷移而不是在我自己的代碼中執行此操作?

在控制檯中鍵入$Error[0].Exception顯示異常消息,但是如何列出其他屬性?

+0

您是否嘗試將異常轉換爲System.Reflection.ReflectionTypeLoadException並訪問LoaderExceptions屬性? – user1908061

+0

@ user1908061不,我應該怎麼做?在PowerShell命令行中?在類文件中?怎麼樣?這可能是答案,在這種情況下,如果您將它作爲一個更詳細的答案發布,我會很感激。 –

+0

我把它在控制檯中輸入,就像你用'$ Error [0] .Exception'所做的那樣,但是這次把異常轉換爲正確的類型並訪問'LoaderExceptions'屬性來獲得更多有關確切問題的信息。但我很高興你已經解決了這個問題。 :-) – user1908061

回答

3

我不知道,如果這能幫助你,但只是拋出一個想法...

你可以嘗試運行從代碼遷移 - 有你可能會尤爲明顯異常處理。例如

EF Code First DbMigration without nuget

DbMigrator migrator = new DbMigrator(new YourConfiguration()); 
migrator.Update(); // or update specific migrations..  

你也可以把它通過配置 - 甚至把那對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> 
+0

我創建了一個新的控制檯應用程序項目,引用了我的遷移項目幷包含您發佈的代碼。當它運行時,我能夠看到異常並找出哪個程序集未能加載。謝謝。 –

+1

我不知道它會工作 - 但很高興它:)不客氣@FernandoCorreia – NSGaga