我使用实体框架,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)

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


当前回答

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

其他回答

我能够通过在项目中所有引用的DLL文件上标记“复制本地=True”来修复这个问题,在测试服务器上重新构建和部署。

最初我尝试了Fusion日志查看器,但没有帮助 所以我最终使用WinDbg与SOS扩展。

!dumpheap -stat -type异常/D

然后我检查了FileNotFoundExceptions。异常中的消息包含未加载的DLL的名称。

注意,/D会给你超链接的结果,所以点击FileNotFoundException摘要中的链接。这将显示一个例外列表。然后点击其中一个例外的链接。那将!dumpobject异常。然后,您应该能够单击异常对象中的Message链接,然后您将看到文本。

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

我解决这个问题的方法是创建一个控制台应用程序,其中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脚本。这在一个特定的场景中帮助了我。

这个错误没有真正的灵丹妙药。关键是要有理解问题的所有信息。动态加载的程序集很可能缺少引用的程序集。该程序集需要位于应用程序的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.
}