[Web API series tutorial] 3.2-practice: process data (add model and controller)

Source: Internet
Author: User

[Web API series tutorial] 3.2-practice: process data (add model and controller)
Preface

In this section, you will add a model class for defining database entities. Then you will add a Web API controller that is used to execute CRUD (Create, Retrieve, Update, Delete -- Translator's note) operations on these entities.

Add model class

In this tutorial, we will use the "Code First" method to create a database for the Entity Framework (EF. For Code First, you write the C # class to create the corresponding database table and use EF to create the database. (For more information, see Entity Framework Development Approaches .)

First, we define our domain object as POCO. We will create the following POCO:
Author
Book

In solution resource management, right-click the Models folder. Select Add and then Class. The class named Author.

Replace all the sample codes in Author. cs with the following code.

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

Add another class named Book and replace it with the following code.

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

Entity Framework will use these models to create database tables. For each model, the Id attribute becomes the primary key column of the database table.

In the Book class, AuthorId defines a foreign key to the Author table. (For simplicity, I assume that each book has only one author .) This book class also contains a navigation attribute for the relevant Author. You can use the navigation property to access relevant authors in the code. In [Web API series tutorial] 3.4-practice: process data (process Object Relationships)
Describes more information about navigation properties.

Add a Web API Controller

In this section, we will add a Web API controller that supports CRUD (create, read, update, and delete. These controllers use the Entity Framework to communicate with the database layer.

First, you should delete the ValuesControllers. cs file under the Controllers directory. This file contains a Web API example, but you do not need it for this tutorial.

Then, compile the project. The Web API framework uses reflection to discover this model class, so it needs to compile the assembly.

In Solution Explorer, right-click the Controllers folder. Select Add and then Controller.

In the Add Scaffold dialog box, select "Web API 2 controller with actions, using Entity Framework ". Click Add. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPjxpbWcgYWx0PQ = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/160301/042259CL-3.png" title = "\"/>

In the Add Controller dialog box, perform the following operations:
1. In the model class drop-down box, select the Author class. (If you do not see it in the drop-down list, make sure that the project has been compiled .)
2. Select "Use async controller action ".
3. Keep the Controller name as "AuthorsController ".
4. Click the plus sign (+) to go to Data Context Class.

In the New Data Context dialog box, retain the default name and click Add.

Click Add to complete the Add Controller dialog box. This dialog will add two classes to your project:
AuthorsController defines a Web API controller. This controller implements the rest api, which is used by the client to perform CRUD operations on the authors list.
BookServiceContext manages entity objects at runtime, including gathering object data, tracing, and retaining data from the database to the database. It inherits from DBContext.

Compile the project again on this node. Now, add the API controller for the Book object in the same step. This time, select Book as the model class, and select an existing BookServiceContext class as the data context class. (Do not create a new data context .) Click Add to Add the controller.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.