Preface
In this section, you will use Code First migration (http://msdn.microsoft.com/en-us/data/jj591621) on the EF to build the database with the test data.
Select Library Package Manager under the Tools directory and 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 more than one context type appears in Bookservice, enter "Enable-migrations–contexttypename BookService.Models.BookServiceContext", please see. --Translator's note.
Open the Configuration.cs file. Add the following using statement.
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,NewAuthor () {Id =1, Name ="Jane Austen"},NewAuthor () {Id =2, Name ="Charles Dickens"},NewAuthor () {Id =3, Name ="Miguel de Cervantes"} ); Context. Books.addorupdate (x = x.id,NewBook () {Id =1, Title ="Pride and Prejudice", year =1813, Authorid =1, Price =9.99M, Genre ="Comedy of Manners"},NewBook () {Id =2, Title ="Northanger Abbey", year =1817, Authorid =1, Price =12.95M, Genre ="Gothic parody"},NewBook () {Id =3, Title ="David Copperfield", year =1850, Authorid =2, Price = the, Genre ="Bildungsroman"},NewBook () {Id =4, Title ="Don Quixote.", year =1617, Authorid =3, Price =8.95M, Genre ="Picaresque"} );}
In the Package Manager console window, type the following command:
Add-Migration InitialUpdate-Database
The first command generates the code used to create the database, and the second command executes those code. The database uses LocalDB and is created locally.
Discovery API (optional)
Press F5 to run the application in debug mode. Visual Studio launches IIS Express and runs your web app. Visual Studio launches a browser and opens the app's home page.
When Visual Studio runs this Web project, it is given a port number. In, the port number is 50524. When you run the application, you may see different port numbers.
The home page is implemented using ASP. There is a link at the top of the page that says "API". This link will take you to an automatically generated help page about the Web API. (Want to know how this help page is generated, and how you can add your own documents to the page, view the creating helps pages for ASP. NET Web API (http://www.asp.net/web-api/overview/ Creating-web-apis/creating-api-help-pages). You can click the links on the help page to see the details of the API, including the request and the appropriate format.
The API supports CRUD operations on the database. The following table is a summary of the API.
Authors |
Notes |
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 |
Notes |
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 creates the database and calls the seed method. When you execute 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". Leave the authentication option as "Windows Authentication". Click Connect.
Visual Studio connects to LocalDB and displays the database that already exists in the SQL Server Object Explorer window. You can expand the node to see the tables created by EF.
To see the data, right-click a table and select View data.
The results of the books table are shown below. Note that EF aggregates the database through the seed data, and the table contains foreign keys to the authors table.
"Web API Series Tutorial" 3.3-Combat: Process data (Build database)