From befdbbaa28f5c116484cf742093a066c1f5342cf Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Sat, 24 Jul 2021 07:16:19 -0700 Subject: [PATCH] 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 --- aspnetcore/data/ef-rp/intro.md | 10 ++++++---- aspnetcore/data/ef-rp/intro/samples/cu50/Startup.cs | 3 +++ .../data/ef-rp/intro/samples/cu50/StartupSQLite.cs | 3 ++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/aspnetcore/data/ef-rp/intro.md b/aspnetcore/data/ef-rp/intro.md index d6334303f0..bb6ebeeb07 100644 --- a/aspnetcore/data/ef-rp/intro.md +++ b/aspnetcore/data/ef-rp/intro.md @@ -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. + + ### Add the database exception filter -Add to `ConfigureServices` as shown in the following code: +Add and 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 diff --git a/aspnetcore/data/ef-rp/intro/samples/cu50/Startup.cs b/aspnetcore/data/ef-rp/intro/samples/cu50/Startup.cs index c2a31cbfc2..884ed5c332 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu50/Startup.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu50/Startup.cs @@ -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 } } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu50/StartupSQLite.cs b/aspnetcore/data/ef-rp/intro/samples/cu50/StartupSQLite.cs index b077f54e49..36940be466 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu50/StartupSQLite.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu50/StartupSQLite.cs @@ -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 } }