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

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


当前回答

将我的特定问题/解决方案添加到此,因为这是此错误消息的第一个结果。在我的例子中,当我在IIS中第一个应用程序的文件夹中部署第二个应用程序时收到了这个错误。两者都定义了具有相同名称的连接字符串,导致子应用程序有冲突,并反过来生成这个(对我来说)不明显的错误消息。它通过添加:

<clear/>

在子web应用程序的连接字符串块中,阻止它继承web的连接字符串。配置文件在层次结构中更高,所以它看起来像:

<connectionStrings>
  <clear/>
  <add name="DbContext" connectionString="MySERVER" providerName="System.Data.SqlClient" />
</connectionStrings>

一个参考堆栈溢出问题,帮助一旦我确定什么是 子应用程序会从父应用程序web.config继承吗?

其他回答

两种可能的解决方案:

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

我在使用ASP时遇到了这个错误。NET 4 + SQL Server 2008 R2 +实体框架4应用程序。

它在我的开发机器(Windows Vista 64位)上工作得很好。然后,当部署到服务器(Windows server 2008 R2 SP1)时,它将一直工作到会话超时。因此,我们部署应用程序,一切看起来都很好,然后让它超过20分钟的会话超时,然后抛出这个错误。

为了解决这个问题,我使用Ken Cox博客上的代码来检索LoaderExceptions属性。

对于我的情况,缺失的DLL是Microsoft.ReportViewer.ProcessingObjectModel(版本10)。这个DLL需要安装在运行应用程序的机器的GAC中。您可以在微软下载站点上的Microsoft Report Viewer 2010 Redistributable Package中找到它。

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

我改变了引用的特定版本属性为假,这有助于。

如果部署到IIS,请确保允许在IIS上使用32位应用程序。您可以在当前应用程序池的设置上定义这一点。