问题
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()
回答1:
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.
回答2:
If you created your project using the ASP.NET 5 Starter Web template, ASP.NET Identity has included a temporary workaround to create their database automatically using Migrations in the ApplicationDbContext
constructor:
private static bool _created = false;
public ApplicationDbContext()
{
// Create the database and schema if it doesn't exist
// This is a temporary workaround to create database until Entity Framework database migrations
// are supported in ASP.NET 5
if (!_created)
{
Database.AsMigrationsEnabled().ApplyMigrations();
_created = true;
}
}
To disable Migrations, remove their workaround. :)
Otherwise, in EF7, we'll never run Migrations or any other database creation logic unless your application (or the starter template) calls methods to do so.
来源:https://stackoverflow.com/questions/28465631/in-an-asp-net-vnext-site-why-does-a-migrations-related-exception-appear-at-star