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

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


当前回答

在Nuget控制台中输入迁移命令时,我遇到了实体框架的这个问题。

当我把我的OAuthAuthorizationServerProvider代码从我的应用程序移动到一个包含核心数据访问逻辑以及我的DBContext类的类库项目时,问题就出现了。

我检查类库项目引用的所有dll。对于所有这些(除了.net系统dll) CopyToLocal是真的,我完全困惑。

I knew that there was something wrong with DLLs themselves not my codes. I checked them again and I noticed that when I moved my ApplicationOauthProvider class into a class library project the ApplicationOauthProvider inherits from OAuthAuthorizationServerProvider class which is located in Microsoft.Owin.Security.OAuth assembly, I checked it's package version and suddenly noticed that the version of package that I used for the class library project (not my application project) is very old 2.1, but on my application the latest version was installed (3.0.1) so I upgraded version of the Microsoft.Owin.Security.OAuth package from Nuget fo my class library project and problem got away

简而言之,在检查dll的copytollocal属性后,也检查它们的版本,并将旧版本更新到最新版本

其他回答

两种可能的解决方案:

您正在以发布模式编译,但从Debug目录部署了较旧的编译版本(反之亦然)。 您的测试环境中没有安装正确的. net Framework版本。

在配置文件中将32位IIS模式设置为true,调试模式设置为true,删除临时目录并重置IIS可以暂时修复问题,一段时间后它会恢复。

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

我为SharePoint构建了一些项目,当然,也部署了它们。有一次发生了。

我在C:\Windows\assembly\temp\xxx(使用FarManager)中找到了一个旧的程序集,在重新启动后删除了它,并构建了所有项目。

我对MSBuild有疑问,因为在项目程序集中链接的项目和每个程序集都标记为“复制本地”,但不是从GAC。

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