Controllers (Cont.)


Go to the Package Manager Console located at


Enter the following command:

Add-Migration BookStore1.Data.BookStore1Context

This will create the classes for supporting migrations.



 


Now, we need to apply those changes to the database. Before that, make sure the connection string specified in the file points to the database server that we want to connect:
   {
    "Logging": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.AspNetCore": "Warning"
      }
    },
    "AllowedHosts": "*",
    "ConnectionStrings": {
       "BookStoreWithDBContext":
       "Server=localhost\\XQLEXPRESS03;
         Database=BookStore1Context-0b35f143-69b8-490c-80cf-b122206efbdd;
         User ID=wenchen;
         Password=database;
         Encrypt=False;
         Trusted_Connection=True;
         MultipleActiveResultSets=true"
     }
   }
By default, the scaffolding process makes it point to the SQL Server Express local DB. For applying the changes to the database, let’s run the following command:

Update-Database


This will update the database based on our models:





      On birthdays during his childhood:    
      “When I was 10 there wasn’t trampolines and cartoon characters,    
      I never went to Chuck E. Cheese!    
      My mom said, ‘You wanna see a mouse,    
      pull the refrigerator out!’”    
      — George Lopez    






C:\ASP.NET-workspace\BookStore1\Views\Home\HomeController.cs
01@model BookStore1.Models.Book
02 
03@{ ViewData["Title"] = "Details"; }
04 
05<h1>Details</h1>
06<div>
07  <h4>Book</h4><hr />
08  <dl class="row">
09    <dt class="col-sm-2">
10      @Html.DisplayNameFor( model => model.Title )
11    </dt>
12    <dd class="col-sm-10">
13      @Html.DisplayFor( model => model.Title )
14    </dd>
15    <dt class="col-sm-2">
16      @Html.DisplayNameFor( model => model.Genre )
17    </dt>
18    <dd class="col-sm-10">
19      @Html.DisplayFor( model => model.Genre )
20    </dd>
21    <dt class="col-sm-2">
22      @Html.DisplayNameFor( model => model.Price )
23    </dt>
24    <dd class="col-sm-10">
25      @Html.DisplayFor( model => model.Price )
26    </dd>
27    <dt class="col-sm-2">
28      @Html.DisplayNameFor( model => model.PublishDate )
29    </dt>
30    <dd class="col-sm-10">
31      @Html.DisplayFor( model => model.PublishDate )
32    </dd>
33  </dl>
34 
35  <table>
36    <thead>
37      <tr><th>Authors</th></tr>
38    </thead>
39    <tbody>
40      @foreach ( var item in Model.Authors ) {
41        <tr><td>
42          @Html.DisplayFor( modelItem => item )
43        </td></tr>
44      }
45    </tbody>
46  </table>
47</div><hr />
48  
49<div>
50  <a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
51  <a asp-action="Index">Back to List</a>
52</div>

@ViewData is a way to access the properties of ViewData, which is a dictionary-like object used to pass data from a controller to its corresponding view.
Right-click Home (Views) ⇒ Add ⇒ New Item... ⇒ Razor View–Empty







      “A lot of Americans are suffering from depression younger and younger —    
      our children are seeing the sippy cup as half-empty.”    
      — Maria Bamford