In an ASP.NET Core 10 app using EF Core, what do AddDatabaseDeveloperPageExceptionFilter and UseMigrationsEndPoint do, and why are they Development-only?
answer
- package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
- plugs into the developer exception page
- DbException plus pending migrations
- POST /ApplyDatabaseMigrations returns 204
- replaces the obsolete UseDatabaseErrorPage
basics
~20 sAddDatabaseDeveloperPageExceptionFilter makes the developer exception page explain database errors caused by pending migrations; UseMigrationsEndPoint handles its Apply Migrations button by running MigrateAsync. Both are Development-only because an HTTP request changing the schema must never reach production.
solid answer
~40 sBoth come from the `Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore` package and appear in the MVC, Razor Pages and Blazor Web App templates with Individual auth. `builder.Services.AddDatabaseDeveloperPageExceptionFilter()` registers an `IDeveloperPageExceptionFilter`: when a request throws a `DbException` and a registered context has pending migrations or model changes, the developer exception page renders an HTML page listing them, with the `dotnet ef` commands and an Apply Migrations button. `app.UseMigrationsEndPoint()` adds middleware listening on `/ApplyDatabaseMigrations` by default; a POST with a `context` form field naming the context type resolves it from the request services, calls `Database.MigrateAsync()` and returns 204. The template wraps it in `if (app.Environment.IsDevelopment())`, because exposing schema changes and error details over HTTP in production is a security hole. In ASP.NET Core 10 the older `UseDatabaseErrorPage` middleware is still shipped but marked `[Obsolete]` in favour of the filter.
go deeper
Recognise both template lines, say that one improves the Development error page and the other applies migrations from it, and that neither belongs in production.
Explain the flow: a DbException plus pending migrations triggers the filter's page, its button posts to /ApplyDatabaseMigrations, and the middleware runs MigrateAsync and returns 204.
Argue why schema changes never run from an HTTP request in production, and where migrations are applied instead in a reviewed deployment.
Decide how far developer conveniences like this may go in shared test environments, and what guard keeps them out of anything reachable by real users.
## Where these two calls come from When you create an MVC, Razor Pages or Blazor Web App project with local Individual-account authentication, the template wires EF Core into Identity and adds two diagnostics helpers from the **`Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore`** package: ```csharp builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString)); builder.Services.AddDatabaseDeveloperPageExceptionFilter(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseMigrationsEndPoint(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } ``` They exist for one recurring local-development moment: you pulled a branch with a new migration, ran the app, and the first query failed because the database schema is behind the model. ## AddDatabaseDeveloperPageExceptionFilter This is a **service** registration. It adds `DatabaseDeveloperPageExceptionFilter` as a singleton `IDeveloperPageExceptionFilter`, an extension point of the developer exception page that `WebApplication` enables automatically in Development. When a request throws: 1. The filter checks whether the exception, or its inner exception, is a `DbException`. If not, it passes the error to the normal developer page. 2. It looks up every context registered through `DbContextOptions` in the request's services and asks each one for its pending migrations and pending model changes. 3. If any context has pending migrations or model changes, it renders an HTML page that lists them, shows the `dotnet ef migrations add` and `dotnet ef database update` commands (and their Package Manager Console equivalents), and offers an **Apply Migrations** button. 4. Otherwise it falls back to the ordinary developer exception page. Because it inspects contexts from the request services, a context created with `new` outside dependency injection is invisible to it. ## UseMigrationsEndPoint This is **middleware**. It compares every request path with `MigrationsEndPointOptions.Path`, which defaults to `MigrationsEndPointOptions.DefaultPath`, `/ApplyDatabaseMigrations`, and lets every other request through. The Apply Migrations button posts a form to that path with a `context` field holding the context type's assembly-qualified name. The middleware then: - returns 400 with a plain-text message if the field is missing or names a context that is not registered; - otherwise resolves that context from `RequestServices` and calls `Database.MigrateAsync()`; - returns **204 No Content** with `Cache-Control: no-cache,no-store` on success, and rethrows a wrapping `InvalidOperationException` if migrating fails. Without this middleware the button on the error page has nothing to talk to; without the filter, nothing shows the button. ## Why both are Development-only | Concern | What would leak in production | |---|---| | Error detail | migration names, context types and database error text shown to any visitor | | Schema changes | any client that can POST a form could trigger `MigrateAsync` against the live database | | Process | schema changes would bypass the reviewed deployment step that should own them | The XML docs on both types say they should only be enabled in Development, and the template enforces it with the `IsDevelopment()` branch. Note that `AddDatabaseDeveloperPageExceptionFilter()` itself is registered unconditionally in the template; that is harmless because the developer exception page that consults it only runs in Development. Production applies migrations through a deployment step such as `dotnet ef database update` or a migration bundle. ## Configuring and limits Both pieces are small, but they have a few knobs and edges worth knowing: - `UseMigrationsEndPoint(new MigrationsEndPointOptions { Path = "/dev/migrate" })` moves the endpoint; the error page builds its button URL from `DatabaseErrorPageOptions.MigrationsEndPointPath`, so configure that through `builder.Services.Configure<DatabaseErrorPageOptions>(...)` to the same value. - `AddDatabaseDeveloperPageExceptionFilter`, the filter class and the migrations middleware's request handling are marked `[RequiresDynamicCode]` with the message that DbContext migrations operations are not supported with Native AOT, so they have no place in an AOT-published API. - The page lists every registered context, so an app with a booking context and a reporting context sees pending migrations for each one separately. ## The obsolete predecessor Older tutorials call `app.UseDatabaseErrorPage()`. In ASP.NET Core 10 that middleware still ships but carries `[Obsolete]` with the message to use `DatabaseDeveloperPageExceptionFilter` instead. Some docs pages still list it in a middleware-order example; the source and the templates are what count, so new code uses the filter and `UseMigrationsEndPoint`.
- You added AddDatabaseDeveloperPageExceptionFilter but the Apply Migrations button only shows an error. What is missing?The middleware that answers it. The button posts to `/ApplyDatabaseMigrations`, and only `app.UseMigrationsEndPoint()` handles that path. Without it the POST falls through the rest of the pipeline and never gets the 204 the page waits for, so the page reports that applying migrations failed. Add `UseMigrationsEndPoint()` inside the `IsDevelopment()` branch, and if you changed its path, keep `DatabaseErrorPageOptions.MigrationsEndPointPath` in step.
- Why does the database error page not appear when the failing query used a context created with new?The filter finds contexts by resolving `DbContextOptions` registrations from the request services, then asks each registered context for pending migrations. A context constructed manually is not registered, so the filter finds nothing to report and the normal developer exception page renders instead.
- UseDatabaseErrorPage is still the recommended way to show database errors.
- UseMigrationsEndPoint is safe in production because only admins see the button.
- AddDatabaseDeveloperPageExceptionFilter applies pending migrations automatically at startup.
- The migrations endpoint runs EnsureCreatedAsync instead of applying migrations.
- The filter works without the developer exception page being active.
Assumes ASP.NET Core 10; UseDatabaseErrorPage still ships but is [Obsolete] in favour of DatabaseDeveloperPageExceptionFilter, which the templates use.