add DB exception filter /6 (#22804)

* add DB exception filter /6

* add DB exception filter /6

* add DB exception filter /6

* add DB exception filter /6

* add DB exception filter /6
pull/22847/head
Rick Anderson 2021-07-24 07:16:19 -07:00 committed by GitHub
parent 3bf01c99ff
commit befdbbaa28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 5 deletions

View File

@ -364,13 +364,15 @@ See [Use SQLite for development, SQL Server for production](xref:tutorials/razor
The name of the connection string is passed in to the context by calling a method on a [DbContextOptions](/dotnet/api/microsoft.entityframeworkcore.dbcontextoptions) object. For local development, the [ASP.NET Core configuration system](xref:fundamentals/configuration/index) reads the connection string from the *appsettings.json* file.
<a name="dbx"></a>
### Add the database exception filter
Add <xref:Microsoft.Extensions.DependencyInjection.DatabaseDeveloperPageExceptionFilterServiceExtensions.AddDatabaseDeveloperPageExceptionFilter%2A> to `ConfigureServices` as shown in the following code:
Add <xref:Microsoft.Extensions.DependencyInjection.DatabaseDeveloperPageExceptionFilterServiceExtensions.AddDatabaseDeveloperPageExceptionFilter%2A> and <xref:Microsoft.AspNetCore.Builder.MigrationsEndPointExtensions.UseMigrationsEndPoint%2A> as shown in the following code:
# [Visual Studio](#tab/visual-studio)
[!code-csharp[Main](intro/samples/cu50/Startup.cs?name=snippet_ConfigureServices&highlight=8)]
[!code-csharp[Main](intro/samples/cu50/Startup.cs?name=snippet&highlight=8,15-16)]
Add the [Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore](https://www.nuget.org/packages/Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore) NuGet package.
@ -382,13 +384,13 @@ Install-Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
# [Visual Studio Code](#tab/visual-studio-code)
[!code-csharp[Main](intro/samples/cu50/StartupSQLite.cs?name=snippet_ConfigureServices&highlight=8)]
[!code-csharp[Main](intro/samples/cu50/StartupSQLite.cs?name=snippet_ConfigureServices&highlight=8,16)]
---
The `Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore` NuGet package provides ASP.NET Core middleware for Entity Framework Core error pages. This middleware helps to detect and diagnose errors with Entity Framework Core migrations.
The `AddDatabaseDeveloperPageExceptionFilter` provides helpful error information in the [development environment](xref:fundamentals/environments).
The `AddDatabaseDeveloperPageExceptionFilter` provides helpful error information in the [development environment](xref:fundamentals/environments) for EF migrations errors.
## Create the database

View File

@ -17,6 +17,7 @@ namespace ContosoUniversity
public IConfiguration Configuration { get; }
#region snippet
#region snippet_ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
@ -34,6 +35,7 @@ namespace ContosoUniversity
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
else
{
@ -53,5 +55,6 @@ namespace ContosoUniversity
endpoints.MapRazorPages();
});
}
#endregion
}
}

View File

@ -27,13 +27,13 @@ namespace ContosoUniversity
services.AddDatabaseDeveloperPageExceptionFilter();
}
#endregion
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
else
{
@ -53,5 +53,6 @@ namespace ContosoUniversity
endpoints.MapRazorPages();
});
}
#endregion
}
}