From 111dab7ddfbba1b3497d806a0109ac4de5dfb832 Mon Sep 17 00:00:00 2001 From: Rowan Miller Date: Fri, 16 Oct 2015 09:19:59 -0700 Subject: [PATCH] :bug: No database error page for existing databases When there is an existing database and no migrations are present, then we should just display the standard error page as they are not using migrations and need to adjust their mapping code to match the existing schema. Resolve #182 --- .../DatabaseErrorPageMiddleware.cs | 7 ++++- .../DatabaseErrorPageMiddlewareTest.cs | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) 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()