[Web API series tutorial] 3.3-practice: process data (Create a database)
Preface
In this section, you will use Code First Migration on EF to create a database with test data.
Select Library Package Manager under the Tools directory, and then select Package Manager Console. In the package Management Console window, enter the following command:
Enable-Migrations
This command adds a folder named Migrations to your project, and adds a code file named Configuration. cs to the Migrations folder.
If multiple context types appear in BookService, enter "Enable-Migrations-ContextTypeName BookService. Models. BookServiceContext". For details, see. -- Translator's note.
Open the Configuration. cs file. Add the following using statements.
using BookService.Models;
Then add the following code to the Configuration. Seed method:
protected override void Seed(BookService.Models.BookServiceContext context){ context.Authors.AddOrUpdate(x => x.Id, new Author() { Id = 1, Name = "Jane Austen" }, new Author() { Id = 2, Name = "Charles Dickens" }, new Author() { Id = 3, Name = "Miguel de Cervantes" } ); context.Books.AddOrUpdate(x => x.Id, new Book() { Id = 1, Title = "Pride and Prejudice", Year = 1813, AuthorId = 1, Price = 9.99M, Genre = "Comedy of manners" }, new Book() { Id = 2, Title = "Northanger Abbey", Year = 1817, AuthorId = 1, Price = 12.95M, Genre = "Gothic parody" }, new Book() { Id = 3, Title = "David Copperfield", Year = 1850, AuthorId = 2, Price = 15, Genre = "Bildungsroman" }, new Book() { Id = 4, Title = "Don Quixote", Year = 1617, AuthorId = 3, Price = 8.95M, Genre = "Picaresque" } );}
In the Package Manager Console, type the following command:
Add-Migration InitialUpdate-Database
The first command generates the code used to create the database, and the second command executes the code. The database uses LocalDB and is created locally.
Exploration API (optional)
Press F5 to run the application in debug mode. Visual Studio starts IIS Express and runs your web application. Visual Studio starts a browser and opens the homepage of the app.
When Visual Studio runs this web project, it gives a port number. The port number is 50524. When you run the application, you may see different port numbers.
The homepage is implemented using ASP. net mvc. There is a link at the top of the page that says "API. This link will take you to an automatically generated Web API help page. (To learn how this Help page is generated, and how you add your own documentation to this page, see Creating Help Pages for ASP. NET Web API (http://www.asp.net/web-api/overview/creating-web-apis/creating-api-help-pages ).) You can click the link on the help page to view API details, including requests and corresponding formats.
This API supports CRUD operations on the database. The following table summarizes the APIs.
| Authors |
Remarks |
| GET api/authors |
Get all authors. |
| GET api/authors/{id} |
Get an author by ID. |
| POST/api/authors |
Create a new author. |
| PUT/api/authors/{id} |
Update an existing author. |
| DELETE/api/authors/{id} |
Delete an author. |
| Books |
Remarks |
| GET/api/books |
Get all books. |
| GET/api/books/{id} |
Get a book by ID. |
| POST/api/books |
Create a new book. |
| PUT/api/books/{id} |
Update an existing book. |
| DELETE/api/books/{id} |
Delete a book. |
View database (optional)
When you execute the Update-Database command, EF will create a Database and call the Seed method. After you run the application locally, EF uses LocalDB. You can view the database in Visual Studio. Under the View directory, select SQL Server Object Explorer.
In the Connect to Server dialog box, in the Server Name edit box, type "(localdb) \ v11.0 ". Retain the Authentication option as "Windows Authentication ". Click Connect.
Visual Studio will connect to LocalDB and explain in SQL Server Object? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcmVytLC/2 s/hybrid + 3b/ioaPE47/J0tTVub + quMO92rXjsum/serial =" write image description here "src = "http://www.bkjia.com/uploads/allimg/160301/0422305445-7.png" title = "\"/>
To View the Data, right-click a table and select View Data.
The following shows the results of the Books table. Note that EF aggregates the database through the seed data, and the table contains a foreign key pointing to the Authors table.