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

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


当前回答

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

其他回答

在配置管理器中验证每个项目是否正确设置。

类似于William Edmondson关于这个问题的原因,我将我的配置管理器设置从“调试”“任意CPU”切换到“调试”“。net”。问题是“。”NET的版本没有配置为构建所有的项目,所以我的一些dll是过时的(而另一些是最新的)。这导致启动应用程序时出现许多问题。

临时解决方案是执行Kenny Eliasson的建议,清除\bin和\obj目录。然而,一旦我对非编译项目做了更多的更改,一切都会再次失败。

如果您在项目中使用EntityDataSource,解决方案是Fix:“无法加载一个或多个请求类型”错误。您应该设置ContextTypeName="ProjectNameNameSpace。EntityContainerName”

这解决了我的问题……

这对我很管用。把它添加到你的web.config中

<system.web>
  <trust level="Full" />

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

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

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

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

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

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

我对这个问题的实例最终成为一个缺失的参考。在app.config中引用了一个程序集,但在项目中没有引用。