【Web API系列教程】3.3 — 实战:处理数据(建立数据库)
前言
在本部分中,你將在EF上使用Code First Migration來用測(cè)試數(shù)據(jù)建立數(shù)據(jù)庫。
在Tools目錄下選擇Library Package Manager,然后選擇Package Manager Console。在包管理控制臺(tái)窗口,輸入以下命令:
Enable-Migrations這條命令會(huì)添加一個(gè)名為Migrations的文件夾到你的項(xiàng)目,并添加一個(gè)名為Configuration.cs的代碼文件到Migrations文件夾。
如果在BookService中出現(xiàn)多種上下文類型,請(qǐng)輸入”Enable-Migrations –ContextTypeName BookService.Models.BookServiceContext”,具體請(qǐng)看下圖。——譯者注。
打開Configuration.cs文件。添加以下using語句。
using BookService.Models;然后添加以下代碼到Configuration.Seed方法:
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" }); }在Package Manager Console窗口,鍵入以下命令:
Add-Migration Initial Update-Database第一條命令生成用于創(chuàng)建數(shù)據(jù)庫的代碼,第二條命令執(zhí)行那些代碼。數(shù)據(jù)庫使用LocalDB并創(chuàng)建于本地。
探索API(可選)
按F5在debug模式下運(yùn)行應(yīng)用程序。Visual Studio啟動(dòng)IIS Express并運(yùn)行你的web應(yīng)用。Visual Studio會(huì)啟動(dòng)一個(gè)瀏覽器并打開app的主頁。
當(dāng)Visual Studio運(yùn)行了這個(gè)web項(xiàng)目,它會(huì)給定一個(gè)端口號(hào)。在下圖中,端口號(hào)是50524。當(dāng)你運(yùn)行應(yīng)用程序的時(shí)候,你可能會(huì)看到不同的端口號(hào)。
主頁使用ASP.NET MVC來實(shí)現(xiàn)。在頁面頂部有一個(gè)寫著“API”的鏈接。該鏈接會(huì)帶你去一個(gè)自動(dòng)生成的關(guān)于Web API的幫助頁面。(想了解這個(gè)幫助頁面如何生成的,以及你怎樣添加你自己的文檔進(jìn)該頁面,查看Creating Help Pages for ASP.NET Web API(http://www.asp.net/web-api/overview/creating-web-apis/creating-api-help-pages)。)你可以點(diǎn)擊幫助頁面的鏈接以查看API的詳細(xì)信息,包括請(qǐng)求和相應(yīng)的格式。
該API支持在數(shù)據(jù)庫上執(zhí)行CRUD操作。下表是關(guān)于API的總結(jié)。
| 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. |
| 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. |
查看數(shù)據(jù)庫(可選)
當(dāng)你執(zhí)行了Update-Database命令,EF會(huì)創(chuàng)建數(shù)據(jù)庫并調(diào)用Seed方法。當(dāng)你在本地執(zhí)行了應(yīng)用程序后,EF會(huì)使用LocalDB。你可以在Visual Studio中查看數(shù)據(jù)庫。在View目錄下,選擇SQL Server Object Explorer。
在Connect to Server對(duì)話框中,在Server Name編輯框,鍵入“(localdb)\v11.0”。保留Authentication選項(xiàng)為”Windows Authentication”。點(diǎn)擊Connect。
Visual Studio會(huì)連接到LocalDB并在SQL Server Object Explorer窗口顯示已經(jīng)存在的數(shù)據(jù)庫。你可以展開該節(jié)點(diǎn)查看EF創(chuàng)建的表。
為了看到數(shù)據(jù),右擊一個(gè)表并選擇View Data。
下面的截圖顯示了Books表的結(jié)果。注意EF通過seed數(shù)據(jù)聚集了數(shù)據(jù)庫,并且該表包含了指向Authors表的外鍵。
總結(jié)
以上是生活随笔為你收集整理的【Web API系列教程】3.3 — 实战:处理数据(建立数据库)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 的类和接口的变量调用
- 下一篇: java jdbc 连接mysql数据库