🐛 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:
parent
793504843e
commit
111dab7ddf
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in New Issue