【问题标题】:System.TypeLoadException with Entity Framework 6System.TypeLoadException 与实体框架 6
【发布时间】:2020-05-25 22:31:06
【问题描述】:

在 IIS 中对我的网站进行 deploid 并尝试在浏览器中查看该页面后,我遇到了以下错误。

无法从 程序集“DienstbulletinApp”。描述:未处理的异常 在执行当前 Web 请求期间发生。请 查看堆栈跟踪以获取有关错误和位置的更多信息 它起源于代码。

异常详细信息:System.TypeLoadException:无法加载类型 来自程序集的“DienstbulletinApp.DienstbulletinContext” 'DienstbulletinApp'。

当我在 Visual Studio 2019 中运行我的应用程序时,我没有出现此错误。 我已经从我的 webconfig 文件中删除了上下文,但似乎没有任何解决方案

 <contexts>
      <context type="DienstbulletinApp.DAL.DienstbulletinAppContext, DienstbulletinApp, Version=1.0.0.0, Culture=neutral">
        <databaseInitializer type="DienstbulletinApp.DAL.DienstbulletinAppInitializer, DienstbulletinApp, Version=1.0.0.0, Culture=neutral" />
      </context>
    </contexts>

DienstbulletinContext:

 public class DienstbulletinAppContext : DbContext
    {
        public DienstbulletinAppContext() : base("name=DienstbulletinDBConnectionString")
        {
            Database.SetInitializer(new DienstbulletinAppInitializer());

        }
        public DbSet<Dienstbulletin> Dienstbulletins { get; set; }
        public DbSet<Voertuig> Voertuigen { get; set; }
        public DbSet<Opdracht> Opdrachts { get; set; }
        public DbSet<DienstbulletinDetail> DienstbulletinDetails { get; set; }
        public DbSet<Locatie> Locaties { get; set; }
        public DbSet<Gebruiker> Gebruikers { get; set; }
        public DbSet<Persoon> Personen { get; set; }
        public DbSet<OpdrachtType> OpdrachtTypes { get; set; }
        public DbSet<Aandachtspunt> Aandachtspunten { get; set; }
        public DbSet<Gsm> Gsms { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Persoon>().ToTable("t_Persoon");
            modelBuilder.Entity<Voertuig>().ToTable("t_Voertuig");
            modelBuilder.Entity<Gebruiker>().ToTable("t_Gebruiker");
            modelBuilder.Entity<OpdrachtType>().ToTable("t_OpdrachtType");
            modelBuilder.Entity<Opdracht>().ToTable("t_Opdracht");
            modelBuilder.Entity<Dienstbulletin>().ToTable("t_Dienstbulletin");
            modelBuilder.Entity<DienstbulletinDetail>().ToTable("t_DienstbulletinDetail");
            modelBuilder.Entity<Locatie>().ToTable("t_Locatie");
            modelBuilder.Entity<Aandachtspunt>().ToTable("t_Aandachtspunt");
            modelBuilder.Entity<Gsm>().ToTable("t_Gsm");

            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        }
    }

我正在使用 .Net Framework 版本 4.0.30319 和 ASP.NET 4.7.3535.0

我知道解决方案中必须引用所有 DLL,但出现错误的文件是我自己的应用程序类。

这是完整的错误:

[TypeLoadException:无法从程序集“DienstbulletinApp”加载类型“DienstbulletinApp.DienstbulletinAppContext”。] System.RuntimeTypeHandle.GetTypeByName(字符串名称,布尔 throwOnError,布尔 ignoreCase,布尔反射,StackCrawlMarkHandle stackMark,IntPtr pPrivHostBinder,布尔 loadTypeFromPartialName,ObjectHandleOnStack 类型)+0 System.RuntimeTypeHandle.GetTypeByName(字符串名称,布尔 throwOnError,布尔 ignoreCase,布尔反射,StackCrawlMark 和 stackMark,IntPtr pPrivHostBinder,布尔 loadTypeFromPartialName)+95 System.Type.GetType(String typeName, Boolean throwOnError) +63 System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func1 initializerArgs, Func3 exceptionMessage) +46 [InvalidOperationException:无法为应用程序配置中指定的 DbContext 类型“DienstbulletinApp.DienstbulletinAppContext,DienstbulletinApp”设置“DienstbulletinApp.DienstbulletinAppInitializer,DienstbulletinApp”类型的数据库初始化程序。条目的格式应为“key="DatabaseInitializerForType MyNamespace.MyDbContextClass, MyAssembly" value="MyNamespace.MyInitializerClass, MyAssembly"' 或 'key="DatabaseInitializerForType MyNamespace.MyDbContextClass, MyAssembly" value="Disabled"'。还要验证数据库初始值设定项构造函数的每个参数是否存在“DatabaseInitializerArgumentForType”条目。有关详细信息,请参阅内部异常。考虑使用配置部分来设置数据库初始化器(http://go.microsoft.com/fwlink/?LinkID=237468).] System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func1 initializerArgs, Func3 exceptionMessage) +327 System.Data.Entity.Internal.InitializerConfig.TryGetInitializerFromLegacyConfig(类型 contextType)+644 System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type contextType) +39 System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServiceFactory(类型类型,字符串名称)+513 System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) +87 System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetService(类型类型,对象键)+187 System.Linq.WhereSelectArrayIterator2.MoveNext() +78 System.Linq.Enumerable.FirstOrDefault(IEnumerable1 source, Func2 predicate) +115 System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver2.GetService(Type type, Object key) +41 System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +159 System.Data.Entity.Internal.RetryAction1.PerformAction(TInput input) +171 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action1 动作)+269 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +20 System.Data.Entity.Internal.Linq.InternalSet1.Initialize() +69 System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext() +21 System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider() +59 System.Linq.Queryable.FirstOrDefault(IQueryable1 源,Expression1 predicate) +61 DienstbulletinApp.Controllers.GebruikerController.Login() in D:\Plastic\Dienstbulletin\Dienstbulletin\Controllers\GebruikerController.cs:24 lambda_method(Closure , ControllerBase , Object[] ) +87 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 参数)+35 System.Web.Mvc.Async.c.b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +39 System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0() +80 System.Web.Mvc.Async.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2() +387 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +50 System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +188 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38 System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +26 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +68 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38 System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +40 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +68 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +602 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep 步骤) +195 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128

【问题讨论】:

  • 当您将代码推送到 IIS 时,您能否确认 .DLL 正在发布?确保 DLL 引用 DienstbulletinApp.DienstbulletinContext 的属性 Copy Local 设置为 true。
  • @salli 你的意思是“复制到输出目录”设置为真。那会生成一个DLL吗? DienstbulletinApp.DienstbulletinContext 只是我解决方案中的一个类
  • 啊,我明白了。是的,对不起,我误会了。我认为该错误可能是由于未能初始化上下文引起的。尝试将Database.SetInitializer(new DienstbulletinAppInitializer()); 更改为System.Data.Entity.Database.Database.SetInitializer(new DienstbulletinAppInitializer());,如this stackoverflow post中所述。 stackoverflow.com/questions/36953401/…
  • 在某些情况下,当数据库发生错误时,可能会出现错误消息。你检查过你的数据库日志吗?因为你的应用用户已经从登录用户变成了应用池身份
  • @Jokies Ding 数据库日志确实有登录失败的错误。添加 'IIS APPPOOL\.NET v4.5' 用户后,sql 日志中的错误消失了,但我的浏览器中的错误仍然存​​在。

标签: asp.net-mvc iis entity-framework-6


【解决方案1】:

发现从上下文中删除版本和文化=中性的错误解决了问题:

<contexts>
  <context type="DienstbulletinApp.DAL.DienstbulletinAppContext, DienstbulletinApp">
    <databaseInitializer type="DienstbulletinApp.DAL.DienstbulletinAppInitializer, DienstbulletinApp" />
  </context>
</contexts>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多