Trong bài này, chúng ta sẽ thêm một trường (thuộc tính) mới đến lớp Book và trường này sẽ được ánh xạ tự động đến cơ sở dữ liệu bằng cách dùng EF Core Code First Migrations.
Thêm thuộc tính Rating đến lớp Book
Để xếp hạng một cuốn sách theo bình chọn của độc giả, chúng ta thêm một thuộc tính tên Rating đến lớp Book trong tập tin Models / Book.cs:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace MVCBooks.Models
{
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
[Column(TypeName = "decimal(18, 2)")]
public decimal Price { get; set; }
public string Rating { get; set; }
}
}
Vì một thuộc tính mới được thêm đến lớp Book nên chúng ta cũng cần cập nhật thuộc tính này đến danh sách của thuộc tính [Bind] từ các phương thức Create và Edit trong tập tin BooksController.cs:
public async Task<IActionResult> Create([Bind("Id,Title,ReleaseDate,Genre,Price, Rating")] Book book)
public async Task<IActionResult> Edit(int id, [Bind("Id,Title,ReleaseDate,Genre,Price, Rating")] Book book)
Bên cạnh đó, chúng ta cũng phải cập nhật thuộc tính Rating đến các giao diện hiển thị, tạo mới và chỉnh sửa thông tin của ứng dụng.
Mở tập tin Views / Books / Index.cshtml và thêm Rating:
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Books[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Books[0].ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Books[0].Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Books[0].Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Books[0].Rating)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Books)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
Mở tập tin Views / Books / Create.cshtml
<div class="form-group">
<label asp-for="Genre" class="control-label"></label>
<input asp-for="Genre" class="form-control" />
<span asp-validation-for="Genre" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Price" class="control-label"></label>
<input asp-for="Price" class="form-control" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Rating" class="control-label"></label>
<input asp-for="Rating" class="form-control" />
<span asp-validation-for="Rating" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
Tương tự thêm Rating cho các tập tin Edit.cshtml, Delete.cshtml và Details.cshtml trong thư mục Views / Books.
Mở tập tin SeedData.cs trong thư mục Models để thêm giá trị đến trường mới cho hai đối tượng mẫu:
new Book
{
Title = "Atomic Habits",
ReleaseDate = DateTime.Parse("2018-10-16"),
Genre = "Self-Help",
Price = 11.98M,
Rating = "R"
},
new Book
{
Title = "Dark Roads",
ReleaseDate = DateTime.Parse("2021-8-3"),
Genre = "Novel",
Price = 18.59M,
Rating = "R"
}
Nếu thực thi chương trình lúc này sẽ phát sinh lỗi vì Model (lớp Book) khác với lược đồ cơ sở dữ liệu. Chúng ta phải dùng EF Core Migrations ánh xạ thuộc tính mới (Rating) sang lược đồ cơ sở dữ liệu bằng cách vào Tools chọn NuGet Package Manager > Package Manager Console và gõ lệnh sau đây vào PMC:
Add-Migration Rating
Update-Database
Kết quả

Bây giờ có thể thực thi ứng dụng. Chúng ta sẽ thấy xuất hiện cột Rating nhưng giá trị cột này vẫn chưa cập nhật. Xóa tất cả dữ liệu hiện có bằng cách nhấn liên kết Delete và đóng ứng dụng. Thực thi lại ứng dụng sẽ cập nhật các giá trị cho cột Rating:
