diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageMiddleware.cs b/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageMiddleware.cs index 18c51ff3b9..bad1698582 100644 --- a/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageMiddleware.cs +++ b/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageMiddleware.cs @@ -92,7 +92,12 @@ namespace Microsoft.AspNet.Diagnostics.Entity select m.Key) .ToList(); - var pendingModelChanges = modelDiffer.HasDifferences(migrationsAssembly.ModelSnapshot?.Model, dbContext.Model); + // HasDifferences will return true if there is no model snapshot, but if there is an existing database + // and no model snapshot then we don't want to show the error page since they are most likely targeting + // and existing database and have just misconfigured their model + var pendingModelChanges = migrationsAssembly.ModelSnapshot == null && databaseExists + ? false + : modelDiffer.HasDifferences(migrationsAssembly.ModelSnapshot?.Model, dbContext.Model); if ((!databaseExists && pendingMigrations.Any()) || pendingMigrations.Any() || pendingModelChanges) { diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs index f1d1503e32..1caded838b 100644 --- a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs +++ b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs @@ -72,6 +72,34 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests } } + [ConditionalTheory] + [FrameworkSkipCondition(RuntimeFrameworks.Mono)] + public async Task Existing_database_not_using_migrations_exception_passes_thru() + { + TestServer server = SetupTestServer(); + var ex = await Assert.ThrowsAsync(async () => + await server.CreateClient().GetAsync("http://localhost/")); + + Assert.Equal("Invalid column name 'Name'.", ex.InnerException.Message); + } + + class DatabaseErrorButNoMigrationsMiddleware + { + public DatabaseErrorButNoMigrationsMiddleware(RequestDelegate next) + { } + + public virtual Task Invoke(HttpContext context) + { + var db = context.ApplicationServices.GetService(); + db.Database.EnsureCreated(); + db.Database.ExecuteSqlCommand("ALTER TABLE dbo.Blog DROP COLUMN Name"); + + db.Blogs.Add(new Blog()); + db.SaveChanges(); + throw new Exception("SaveChanges should have thrown"); + } + } + [ConditionalTheory] [FrameworkSkipCondition(RuntimeFrameworks.Mono)] public async Task Error_page_displayed_no_migrations()