我使用实体框架,SQL Server 2000, Visual Studio 2008和企业库开发了一个应用程序。

它在本地工作得非常好,但是当我将项目部署到我们的测试环境时,我得到了以下错误:

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information Stack trace: at System.Reflection.Module._GetTypesInternal(StackCrawlMark& stackMark) at System.Reflection.Assembly.GetTypes() at System.Data.Metadata.Edm.ObjectItemCollection.AssemblyCacheEntry.LoadTypesFromAssembly(LoadingContext context) at System.Data.Metadata.Edm.ObjectItemCollection.AssemblyCacheEntry.InternalLoadAssemblyFromCache(LoadingContext context) at System.Data.Metadata.Edm.ObjectItemCollection.AssemblyCacheEntry.LoadAssemblyFromCache(Assembly assembly, Boolean loadReferencedAssemblies, Dictionary2 knownAssemblies, Dictionary2& typesInLoading, List`1& errors) at System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly, Boolean loadReferencedAssemblies) at System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyForType(Type type) at System.Data.Metadata.Edm.MetadataWorkspace.LoadAssemblyForType(Type type, Assembly callingAssembly) at System.Data.Objects.ObjectContext.CreateQuery[T](String queryString, ObjectParameter[] parameters)

实体框架似乎有问题,任何线索如何修复它?


当前回答

我在编译Visual Studio包(VSPackage)时报告了相同的错误消息。编译整个解决方案,当CreatePkgDef创建包时抛出错误。话虽如此,很明显,我无法捕获loaderexception,因为它不是我的应用程序抛出的,而是微软自己的工具。(虽然我对CreatePkgDef的混乱负责。)

在我的情况下,根本原因是我的解决方案创建了一个MyDll.dll,已经注册到GAC(他们是不同的),所以CreatePgkDef混淆了使用哪个,它决定只是抛出一个错误,这并没有真正的帮助。GAC中的MyDll.dll是由相同产品的安装程序注册的(显然是较早的版本,内容略有不同)。

如何解决

Preferred way: Make sure you use the correct version of MyDll.dll When compiling your project make sure you use a different version number than you used in the previous version located in the GAC. Make sure the following attributes are correct: [assembly: AssemblyVersion("1.0.0.1")] // Assuming the old DLL file was versioned 1.0.0.0 [assembly: AssemblyFileVersion("1.0.0.1")] // Assuming the old DLL file was versioned 1.0.0.0 If needed, specify the fully qualified assembly name (for example, "MyDll.dll, Version=1.0.0.1, Culture=neutral, PublicKeyToken=1234567890abcdef") when you reference it in your other projects. If the above failed: You can uninstall the old MyDll.dll from GAC How to Uninstall an Assembly from the GAC Uninstall the application that includes MyDll.dll

更改AssemblyVersion对我来说已经足够好了。:)

我希望这对你们有帮助。

其他回答

这个错误没有真正的灵丹妙药。关键是要有理解问题的所有信息。动态加载的程序集很可能缺少引用的程序集。该程序集需要位于应用程序的bin目录中。

使用这段代码来确定缺少什么。

using System.IO;
using System.Reflection;
using System.Text;

try
{
    //The code that causes the error goes here.
}
catch (ReflectionTypeLoadException ex)
{
    StringBuilder sb = new StringBuilder();
    foreach (Exception exSub in ex.LoaderExceptions)
    {
        sb.AppendLine(exSub.Message);
        FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
        if (exFileNotFound != null)
        {                
            if(!string.IsNullOrEmpty(exFileNotFound.FusionLog))
            {
                sb.AppendLine("Fusion Log:");
                sb.AppendLine(exFileNotFound.FusionLog);
            }
        }
        sb.AppendLine();
    }
    string errorMessage = sb.ToString();
    //Display or log the error based on your application.
}

在我的情况下,我有一个nuget包,安装在我的项目,但包文件夹从未检入到TFS,因此,在构建机器的nuget包bin文件丢失。因此在生产中我得到了这个错误。我不得不比较bin文件夹在生产和我的本地,然后我发现哪些dll是缺失的,我发现那些属于一个nuget包。

正如之前所提到的,通常情况下,组装不存在。

要确切地知道您丢失了什么程序集,请附加调试器,设置断点,当您看到异常对象时,钻到'LoaderExceptions'属性。丢失的程序集应该在那里。

希望能有所帮助!

当我试图用包管理器控制台添加实体框架迁移时,我也有同样的问题(但在我的本地)。

我解决这个问题的方法是创建一个控制台应用程序,其中Main()有以下代码:

 var dbConfig = new Configuration();
 var dbMigrator = new DbMigrator(dbConfig);
 dbMigrator.Update();

确保Configuration类是失败项目的迁移配置。你需要System.Data.Entity.Migrations来使用DbMigrator。

在应用程序中设置断点,并运行它。异常应该由Visual Studio捕获(除非您将异常类型设置为不中断调试会话),并且您应该能够找到您正在寻找的信息。

在我的案例中,缺少的引用是EFProviderWrapperToolkit。

如果您在PowerShell中引用.dll时遇到此错误,而您的.dll是. net 5(。),那么你应该确保你在PowerShell 6+而不是Windows PowerShell 5.1(或更早的版本)中执行PowerShell脚本。这在一个特定的场景中帮助了我。