Merge pull request #4707 from aspnet/master

Update live with current master
pull/4758/head^2
Rick Anderson 2017-11-01 15:15:10 -10:00 committed by GitHub
commit 89c5705e60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 129 additions and 34 deletions

View File

@ -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

View File

@ -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;
}
}
}
}

View File

@ -6,6 +6,13 @@
<div class="jumbotron">
<h1>Contoso University</h1>
<ul>
<li> <a href="https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/migrations#apply-the-migration-to-the-db">
Apply the migration to the DB </a></li>
<li>
<p>Copy wwwroot from another project to this project.</p>
</li>
</ul>
</div>
<div class="row">
<div class="col-md-4">
@ -27,7 +34,7 @@
<h2>Download it</h2>
<p>You can download the completed project from GitHub.</p>
<p><a class="btn btn-default"
href="https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/cu-final">
href="https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples">
See project source code &raquo;</a></p>
</div>
</div>

View File

@ -0,0 +1,6 @@
@page
@{
var message = TempData["HandleSqlException"] as string;
}
<p>@message</p>

View File

@ -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;

View File

@ -0,0 +1,3 @@
Copy wwwroot from ../cu/ or from a new Razor Pages project.
Run `dotnet ef database update`

View File

@ -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<SchoolContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc().AddSessionStateTempDataProvider();
services.AddMvc();
}

View File

@ -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,

View File

@ -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;
}
}
}
}

View File

@ -0,0 +1,6 @@
@page
@{
var message = TempData["HandleSqlException"] as string;
}
<p>@message</p>

View File

@ -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;

View File

@ -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<SchoolContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
@ -43,6 +41,8 @@ namespace ContosoUniversity
app.UseStaticFiles();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(

View File

@ -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,

View File

@ -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)

View File

@ -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)