Preface
In this section, you will add a model class that defines the database entity. You will then add a Web API controller for performing crud (Create, Retrieve, Update, delete--Translator) operations on these entities.
To add a model class
In this tutorial, we will create the database by using the method of "Code first" for the Entity Framework (EF). For code First, you write the C # class to the corresponding database table, using 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 select Class. This class is named author.
Replace all boilerplate code in Author.cs with the following code.
using System.Collections.Generic;using System.ComponentModel.DataAnnotations;namespace BookService.Models{ publicclass Author { publicintgetset; } [Required] publicstringgetset; } }}
Add another class named book and replace it with the following code.
usingSystem.componentmodel.dataannotations;namespace bookservice.models{ Public classBook { Public intId {Get;Set; } [Required] Public stringTitle {Get;Set; } Public intYear {Get;Set; } Public decimalPrice {Get;Set; } Public stringGenre {Get;Set; }//Foreign Key Public intAuthorid {Get;Set; }//Navigation Property PublicAuthor Author {Get;Set; } }}
The Entity framework uses these models to create database tables. For each model, the ID attribute becomes the primary key column for the database table.
In the book class, Authorid defines a foreign key to the author table. (For simplicity, I assume that there is only one author for each book.) The book class also contains a navigation property to the associated author. You can use the navigation properties to access the relevant author in your code. I've described more about navigation properties in part fourth (handling Entity relations).
Add 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 an example of a Web API, but you don't 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 the Solution Explorer, right-click the Controllers folder. Select Add, and then select Controller.
In the Add Scaffold dialog box, select Web API 2 controller with actions, using Entity Framework. Click Add.
In the Add Controller dialog box, do the following:
1, in the Model class drop-down box, select the author class. (If you don't see it in the drop-down box, make sure the project has been compiled.) )
2, select Use Async controller action.
3, the reserved controller name is "Authorscontroller".
4, click the Plus (+) button next to the data Context Class.
In the new Data Context dialog box, leave 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. The controller implements the rest API, which the client uses to perform CRUD operations on the authors list.
Bookservicecontext manages entity objects at run time, including aggregating object data from the database, tracking, and preserving data to the database. It inherits from DbContext.
On this node, compile the project again. Now go through the same steps to add an API controller for the book entity. This time select book as the Model class and select the Bookservicecontext class that already exists as the data context class. (Do not create a new data context again.) Click Add to add the controller.
Web API Series Tutorial 3.2-Combat: Working with data (adding models and controllers)