🐛 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
This commit is contained in:
Rowan Miller 2015-10-16 09:19:59 -07:00
parent 793504843e
commit 111dab7ddf
2 changed files with 34 additions and 1 deletions

View File

@ -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)
{

View File

@ -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<BloggingContext, DatabaseErrorButNoMigrationsMiddleware>();
var ex = await Assert.ThrowsAsync<DbUpdateException>(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<BloggingContext>();
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()