在方案中找到Models文件夹,右键添加类,命名为Author。

Author.cs 替换以下代码

C#
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace BookService.Models
{
    public class Author
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
    }
}

添加另一个类名Book,如下代码

C#
using System.ComponentModel.DataAnnotations;

namespace BookService.Models
{
    public class Book
    {
        public int Id { get; set; }
        [Required]
        public string Title { get; set; }
        public int Year { get; set; }
        public decimal Price { get; set; }
        public string Genre { get; set; }

        // Foreign Key
        public int AuthorId { get; set; }
        // Navigation property
        public Author Author { get; set; }
    }
}

您可以使用导航属性访问作者的相关代码。

添加 Web API Controllers

这个文件包含一个示例Web API控制器,但是对于本教程中你不需要它。

 

选择Add,然后选择控制器。

它从DbContext继承。

确认都已选择好,点击add

相关文章: