diff --git a/aspnetcore/data/ef-rp/complex-data-model.md b/aspnetcore/data/ef-rp/complex-data-model.md index 8285924afb..b5d470e317 100644 --- a/aspnetcore/data/ef-rp/complex-data-model.md +++ b/aspnetcore/data/ef-rp/complex-data-model.md @@ -27,7 +27,7 @@ The entity classes for the completed data model is shown in the following illust ![Entity diagram](complex-data-model/_static/diagram.png) -If you run into problems you can't solve, download the [completed app for this stage](https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/cu-part5-complex). +If you run into problems you can't solve, download the [completed app for this stage](https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex). ## Customize the data model with attributes diff --git a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Filters/HandleSqlExceptionAttribute.cs b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Filters/HandleSqlExceptionAttribute.cs new file mode 100644 index 0000000000..cc1fc32444 --- /dev/null +++ b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Filters/HandleSqlExceptionAttribute.cs @@ -0,0 +1,34 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using System.Data.SqlClient; + +namespace ContosoUniversity.Filters +{ + public class HandleSqlExceptionAttribute : ExceptionFilterAttribute + { + public override void OnException(ExceptionContext context) + { + var result = new ViewResult + { + ViewName = "Students/Error" + }; + + var exception = context.Exception; + + // If the exception is of the exact type we're seeking, + // populate TempData with instructions for the user. + if (exception is SqlException ) // && ((SqlException)exception).Number == 4060) + { + var msg = "Run update database " + exception.ToString(); + result.TempData = new TempDataDictionary(context.HttpContext, new SessionStateTempDataProvider()) + { + { "HandleSqlException", msg } + }; + + context.Result = result; + context.ExceptionHandled = true; + } + } + } +} \ No newline at end of file diff --git a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Pages/Index.cshtml b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Pages/Index.cshtml index 2a6fd124b1..b42acf2ac9 100644 --- a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Pages/Index.cshtml +++ b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Pages/Index.cshtml @@ -6,6 +6,13 @@

Contoso University

+
@@ -27,7 +34,7 @@

Download it

You can download the completed project from GitHub.

+ href="https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples"> See project source code »

\ No newline at end of file diff --git a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Pages/Students/Error.cshtml b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Pages/Students/Error.cshtml new file mode 100644 index 0000000000..8e0c716e83 --- /dev/null +++ b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Pages/Students/Error.cshtml @@ -0,0 +1,6 @@ +@page +@{ + var message = TempData["HandleSqlException"] as string; +} + +

@message

\ No newline at end of file diff --git a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Pages/Students/Index.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Pages/Students/Index.cshtml.cs index c501f2d75b..aa230b5a3c 100644 --- a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Pages/Students/Index.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Pages/Students/Index.cshtml.cs @@ -1,15 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; +using ContosoUniversity.Filters; +using ContosoUniversity.Models; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; -using ContosoUniversity.Data; -using ContosoUniversity.Models; +using System; +using System.Linq; +using System.Threading.Tasks; namespace ContosoUniversity.Pages.Students { + [HandleSqlException] public class IndexModel : PageModel { private readonly ContosoUniversity.Data.SchoolContext _context; diff --git a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/README.md b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/README.md new file mode 100644 index 0000000000..3373792862 --- /dev/null +++ b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/README.md @@ -0,0 +1,3 @@ +Copy wwwroot from ../cu/ or from a new Razor Pages project. + +Run `dotnet ef database update` \ No newline at end of file diff --git a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Startup.cs b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Startup.cs index 8ced7e46a6..e415b56ab8 100644 --- a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Startup.cs +++ b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/Startup.cs @@ -1,13 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using ContosoUniversity.Data; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using ContosoUniversity.Data; -using Microsoft.EntityFrameworkCore; namespace ContosoUniversity { @@ -25,6 +21,8 @@ namespace ContosoUniversity services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); + services.AddMvc().AddSessionStateTempDataProvider(); + services.AddMvc(); } diff --git a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/appsettings.json b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/appsettings.json index fab63cd2db..5c19341ea6 100644 --- a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/appsettings.json +++ b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=CU-4ss;ConnectRetryCount=0;Trusted_Connection=True;MultipleActiveResultSets=true" + "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=CU-4a;ConnectRetryCount=0;Trusted_Connection=True;MultipleActiveResultSets=true" }, "Logging": { "IncludeScopes": false, diff --git a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Filters/HandleSqlExceptionAttribute.cs b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Filters/HandleSqlExceptionAttribute.cs new file mode 100644 index 0000000000..07f2444a26 --- /dev/null +++ b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Filters/HandleSqlExceptionAttribute.cs @@ -0,0 +1,34 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using System.Data.SqlClient; + +namespace ContosoUniversity.Filters +{ + public class HandleSqlExceptionAttribute : ExceptionFilterAttribute + { + public override void OnException(ExceptionContext context) + { + var result = new ViewResult + { + ViewName = "Students/Error" + }; + + var exception = context.Exception; + + // If the exception is of the exact type we're seeking, + // populate TempData with instructions for the user. + if (exception is SqlException ) // && ((SqlException)exception).Number == 4060) + { + var msg = "Run \"dotnet ef database update\" " + exception.ToString(); + result.TempData = new TempDataDictionary(context.HttpContext, new SessionStateTempDataProvider()) + { + { "HandleSqlException", msg } + }; + + context.Result = result; + context.ExceptionHandled = true; + } + } + } +} \ No newline at end of file diff --git a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Pages/Students/Error.cshtml b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Pages/Students/Error.cshtml new file mode 100644 index 0000000000..8e0c716e83 --- /dev/null +++ b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Pages/Students/Error.cshtml @@ -0,0 +1,6 @@ +@page +@{ + var message = TempData["HandleSqlException"] as string; +} + +

@message

\ No newline at end of file diff --git a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Pages/Students/Index.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Pages/Students/Index.cshtml.cs index c501f2d75b..aa230b5a3c 100644 --- a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Pages/Students/Index.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Pages/Students/Index.cshtml.cs @@ -1,15 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; +using ContosoUniversity.Filters; +using ContosoUniversity.Models; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; -using ContosoUniversity.Data; -using ContosoUniversity.Models; +using System; +using System.Linq; +using System.Threading.Tasks; namespace ContosoUniversity.Pages.Students { + [HandleSqlException] public class IndexModel : PageModel { private readonly ContosoUniversity.Data.SchoolContext _context; diff --git a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Startup.cs b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Startup.cs index 8ced7e46a6..5c26445dd5 100644 --- a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Startup.cs +++ b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/Startup.cs @@ -1,13 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using ContosoUniversity.Data; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using ContosoUniversity.Data; -using Microsoft.EntityFrameworkCore; namespace ContosoUniversity { @@ -22,6 +18,8 @@ namespace ContosoUniversity public void ConfigureServices(IServiceCollection services) { + services.AddSession(); + services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); @@ -43,6 +41,8 @@ namespace ContosoUniversity app.UseStaticFiles(); + app.UseSession(); + app.UseMvc(routes => { routes.MapRoute( diff --git a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/appsettings.json b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/appsettings.json index 82527eca92..fe34252568 100644 --- a/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/appsettings.json +++ b/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part5-complex/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=CU-4sxs;ConnectRetryCount=0;Trusted_Connection=True;MultipleActiveResultSets=true" + "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=CU-5;ConnectRetryCount=0;Trusted_Connection=True;MultipleActiveResultSets=true" }, "Logging": { "IncludeScopes": false, diff --git a/aspnetcore/data/ef-rp/migrations.md b/aspnetcore/data/ef-rp/migrations.md index d82f83e783..09ed58c1fe 100644 --- a/aspnetcore/data/ef-rp/migrations.md +++ b/aspnetcore/data/ef-rp/migrations.md @@ -20,7 +20,8 @@ By [Tom Dykstra](https://github.com/tdykstra) and [Rick Anderson](https://twitte In this tutorial, the EF Core migrations feature for managing data model changes is used. -If you run into problems you can't solve, download the [completed app for this stage](https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/cu-part4-migrations). +If you run into problems you can't solve, download the [completed app for this stage]( +https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations). ## Introduction to migrations @@ -144,7 +145,7 @@ In the command window, enter the following to create the DB and tables. dotnet ef database update ``` -Note: If the `update` command returns the error 'Build failed.`: +Note: If the `update` command returns the error "Build failed.": * Run the command again. * Leave a message at the bottom of the page. @@ -202,6 +203,9 @@ For more information about the PMC commands, see [Package Manager Console (Visua ## Troubleshooting +Download the [completed app for this stage]( +https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part4-migrations). + The app generates the following exception: ```text @@ -212,6 +216,11 @@ Login failed for user 'user name'. Solution: Run `dotnet ef database update` +If the `update` command returns the error "Build failed.": + +* Run the command again. +* Leave a message at the bottom of the page. + >[!div class="step-by-step"] [Previous](xref:data/ef-rp/sort-filter-page) [Next](xref:data/ef-rp/complex-data-model) \ No newline at end of file diff --git a/aspnetcore/data/ef-rp/sort-filter-page.md b/aspnetcore/data/ef-rp/sort-filter-page.md index bd6464f038..89d2e41e23 100644 --- a/aspnetcore/data/ef-rp/sort-filter-page.md +++ b/aspnetcore/data/ef-rp/sort-filter-page.md @@ -23,7 +23,7 @@ The following illustration shows a completed page. The column headings are click ![Students index page](sort-filter-page/_static/paging.png) -If you run into problems you can't solve, download the [completed app for this stage](https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/cu-part3-sorting). +If you run into problems you can't solve, download the [completed app for this stage](https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part3-sorting). ## Add sorting to the Index page @@ -267,7 +267,7 @@ Replace the code in the *Views/Home/About.cshtml* file with the following code: Run the app and navigate to the About page. The count of students for each enrollment date is displayed in a table. -If you run into problems you can't solve, download the [completed app for this stage](https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/cu-part3-sorting). +If you run into problems you can't solve, download the [completed app for this stage](https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part3-sorting). ![About page](sort-filter-page/_static/about.png)