本文介绍了在一个ASP.Net vNext的网站,为什么一个迁移相关的异常在启动时出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有定义迁移,但我仍然看到关于在启动 __ MigrationHistory 表例外,经历了漫长超时后。

我的初始化code是这样的:

 公共无效ConfigureServices(IServiceCollection服务)
{
  services.AddMvc();  services.AddEntityFramework()
    .AddSqlServer()
    .AddDbContext< D​​iContext>(选项=> options.UseSqlServer(Configuration.Get(的ConnectionString)));
}公共无效配置(IApplicationBuilder应用程序)
{
  VAR的LoggerFactory =(ILoggerFactory)app.ApplicationServices.GetService(typeof运算(ILoggerFactory));
  loggerFactory.AddProvider(
      新DiagnosticsLoggerProvider(
        新SourceSwitch(SourceSwitch,信息),
        新ConsoleTraceListener()));  Trace.WriteLine(配置);  app.UseDefaultFiles();
  app.UseStaticFiles();
  app.UseErrorPage();
  app.UseDatabaseErrorPage();
  app.UseMvc(路线=>
  {
    routes.MapRoute(
      名称:默认,
      模板:{控制器} / {行动} / {?ID}
      默认:新{控制器=家,行动=索引});
    });
}

例外:

  Microsoft.Data.Entity.Query.EntityQueryExecutor错误:0:发生在数据存储的一个例外,而迭代查询的结果。
System.Data.SqlClient.SqlException(0x80131904):无效的对象名称__MigrationHistory。
   在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection,Action`1 wrapCloseInAction)
   在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection,Action`1 wrapCloseInAction)
   在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔callerHasConnectionLock,布尔asyncClose)
   在System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,的SqlCommand cmdHandler,SqlDataReader的数据流,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,布尔和放大器; dataReady)
   在System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   在System.Data.SqlClient.SqlDataReader.get_MetaData()
   在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader的DS,RunBehavior runBehavior,字符串resetOptionsString)
   在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,布尔异步,的Int32超时,任务和放大器;任务,布尔asyncWrite,SqlDataReader的DS)
   在System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,字符串的方法,TaskCompletionSource`1完成的Int32超时,任务和放大器;任务,布尔asyncWrite)
   在System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,字符串方法)
   在System.Data.SqlClient.SqlCommand.ExecuteReader(的CommandBehavior行为,串法)
   在System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(的CommandBehavior行为)
   在System.Data.Common.DbCommand.ExecuteReader()
   在Microsoft.Data.Entity.Relational.Query.QueryingEnumerable`1.Enumerator.MoveNext()
   在System.Linq.Enumerable< SelectManyIterator> d__8`2.MoveNext()
   在System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   在Microsoft.Data.Entity.Query.LinqOperatorProvider< _TrackEntities> d__1`2.MoveNext()
   在Microsoft.Data.Entity.Query.EntityQueryExecutor.EnumerableExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()


解决方案

原来,罪魁祸首是 IApplicationBuilder.UseDatabaseErrorPage 。在启动上游SQL错误是造成 Microsoft.AspNet.Diagnostics.Entity 数据库错误页面来呈现。数据库错误页面始终查询数据库的迁移,以检查是否有任何迁移都悬而未决,引发了移民系统尝试产生异常的情况,我的问题查询。

I have no migrations defined but I am still seeing an exception about the __MigrationHistory table at startup, after a lengthy timeout.

My initialization code looks like this:

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();

  services.AddEntityFramework()
    .AddSqlServer()
    .AddDbContext<DiContext>(options => options.UseSqlServer(Configuration.Get("ConnectionString")));
}

public void Configure(IApplicationBuilder app)
{
  var loggerFactory = (ILoggerFactory)app.ApplicationServices.GetService(typeof(ILoggerFactory));
  loggerFactory.AddProvider(
      new DiagnosticsLoggerProvider(
        new SourceSwitch("SourceSwitch", "Information"),
        new ConsoleTraceListener()));

  Trace.WriteLine("Configure.");

  app.UseDefaultFiles();
  app.UseStaticFiles();
  app.UseErrorPage();
  app.UseDatabaseErrorPage();
  app.UseMvc(routes =>
  {
    routes.MapRoute(
      name: "default",
      template: "{controller}/{action}/{id?}",
      defaults: new { controller = "Home", action = "Index" });
    });
}

The exception:

Microsoft.Data.Entity.Query.EntityQueryExecutor Error: 0 : An exception occurred in the data store while iterating the results of a query.
System.Data.SqlClient.SqlException (0x80131904): Invalid object name '__MigrationHistory'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Microsoft.Data.Entity.Relational.Query.QueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.<SelectManyIterator>d__8`2.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.Data.Entity.Query.LinqOperatorProvider.<_TrackEntities>d__1`2.MoveNext()
   at Microsoft.Data.Entity.Query.EntityQueryExecutor.EnumerableExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
解决方案

It turns out the culprit was IApplicationBuilder.UseDatabaseErrorPage. An upstream SQL error at startup was causing the Microsoft.AspNet.Diagnostics.Entity Database Error Page to render. The Database Error Page always queries the database's migrations to check if any migrations are pending, triggering the migrations system to attempt the query that produces the exception in my question.

这篇关于在一个ASP.Net vNext的网站,为什么一个迁移相关的异常在启动时出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:19