【ASP.NET Web API教程】2.3 與實體框架一起使用Web API


2.3 Using Web API with Entity Framework
2.3 與實體框架一起使用Web API

本小節是ASP.NET Web API第2章的第3小節,原文共分為7個部分,分成了7篇文章,故這里也分為7個帖子貼出,以下是本小節的第1部分 — 譯者注。

Part 1: Overview and Creating the Project
第1部分:概述及創建項目

本文引自:http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-entity-framework/using-web-api-with-entity-framework,-part-1

By Mike Wasson | July 3, 2012
作者:Mike Wasson | 日期:2012-7-3

This tutorial shows how to use ASP.NET Web API with ADO.NET Entity Framework, using code-first development.
本教程展示如何以ADO.NET實體框架、采用code-first開發(代碼先行開發模式)來使用ASP.NET Web API。

Entity Framework is an object/relational mapping framework. It maps the domain objects in your code to entities in a relational database. For the most part, you do not have to worry about the database layer, because Entity Framework takes care of it for you. Your code manipulates the objects, and changes are persisted to a database.
實體框架(Entity Framework)是一個對象/關系的映射框架。它把代碼形式的域對象映射成關系數據庫中的實體。在很大程序上,你不必擔心數據庫層,因為實體框架會為你照顧它。你可以對這些對象進行操縱,並把所做的修改持久化到數據庫。

About the Tutorial
關於本教程

In this tutorial, you will create a simple store application. There are two main parts to the application. Normal users can view products and create orders:
在本教程中,你將創建一個簡單的商店應用程序。該應用程序有兩個主要部件。常規用戶可以查看產品並創建訂單(見圖2-8):

WebAPI2-8

圖2-8. ProductStore應用程序的瀏覽界面

注:作者對此應用程序作了簡化,把一個在線商店常規的查看功能(產品列表、購物車、訂單條目、訂單細節)全部顯示在一個Web頁面上 — 譯者注

Administrators can create, delete, or edit products:
管理員可以創建、刪除,或編輯產品(見圖2-9):

WebAPI2-9 圖2-9. ProductStore應用程序的管理界面

注:同樣,作者對此應用程序作了簡化,把一個在線商店的常規管理功能(產品的CRUD操作)全部顯示在一個Web頁面上 — 譯者注

Skills You’ll Learn
將要學習的技能

Here’s what you’ll learn:
以下是你將學習的:

  • How to use Entity Framework with ASP.NET Web API.
    如何以ASP.NET Web API來使用實體框架
  • How to use knockout.js to create a dynamic client UI.
    如何用knockout.js來創建一個動態客戶端UI。
  • How to use forms authentication with Web API to authenticate users.
    如何使用帶有Web API的表單認證來認證用戶。

Although this tutorial is self-contained, you might want to read the following tutorials first:
雖然這個教程是自包含的,但你也許想先閱讀前面的兩個教程:

Some knowledge of ASP.NET MVC is also helpful.
有一些ASP.NET MVC方面的知識也是有幫助的。

Overview
概述

At a high level, here is the architecture of the application:
從一種高級層面上講,以下是該應用程序的體系結構:

  • ASP.NET MVC generates the HTML pages for the client.
    ASP.NET MVC為客戶端生成HTML頁面。
  • ASP.NET Web API exposes CRUD operations on the data (products and orders).
    ASP.NET Web API暴露對數據的CRUD操作(產品和訂單)。
  • Entity Framework translates the C# models used by Web API into database entities.
    實體框架把由Web API使用的C#模型轉換成數據庫實體。

上述體系結構如圖2-10所示(譯者注):

WebAPI2-10

圖2-10. 應用程序的體系結構

The following diagram shows how the domain objects are represented at various layers of the application: The database layer, the object model, and finally the wire format, which is used to transmit data to the client via HTTP.
下圖展示了域對象在應用程序的各個層面上是如何表示的:數據庫層(Database Layer)、對象模型(Object Model),以及最終的線條型格式(Wire Format),該格式用來通過HTTP把數據傳送給客戶端(見圖2-11)。

WebAPI2-11

圖2-11. 域對象在各層面上的表示

說明:由圖2-11可見,在一個應用程序中由EF負責數據庫與域模型對象之間的映射(當然是通過EF的對象關系映射ORM),由Web API負責域對象與線條格式的數據(JSON或XML)之間的轉換,這些數據可以為客戶端頁面提供服務 — 譯者注

Create the Visual Studio Project
創建Visual Studio項目

You can create the tutorial project using either Visual Web Developer Express or the full version of Visual Studio
可以用Visual Web Developer Express(VS的開發者版)或完整版的Visual Studio來創建這個教程項目。

From the Start page, click New Project.
從“開始頁”點擊“新項目”。

In the Templates pane, select Installed Templates and expand the Visual C# node. Under Visual C#, select Web. In the list of project templates, select ASP.NET MVC 4 Web Application. Name the project "ProductStore" and click OK.
在“模板”面板中選擇“已安裝模板”,並展開“Visual C#”節點。選擇其下的“Web”。在項目模板列表中選擇“ASP.NET MVC 4 Web應用程序”。命名此項目為“ProductStore(產品商店)”,並點擊“OK”(見圖2-12)。

WebAPI2-12

圖2-12. 創建ProductStore項目

In the New ASP.NET MVC 4 Project dialog, select Internet Application and click OK.
在“新的ASP.NET MVC 4項目”對話框中選擇“Internet應用程序”,點擊“OK”(見圖2-13)。

WebAPI2-13

圖2-13. 選擇應用程序模板

The "Internet Application" template creates an ASP.NET MVC application that supports forms authentication. If you run the application now, it already has some features:
“Internet應用程序”模板會創建一個支持表單認證的ASP.NET MVC應用程序。如果現在運行此應用程序,它已經具有了一些特性:

  • New users can register by clicking the “Register” link in the upper right corner.
    通過點擊右上角的“注冊”,新用戶可以進行注冊。
  • Registered users can log in by clicking the “Log in” link.
    通過點擊“登錄”鏈接,已注冊用戶可以進行登錄。

Membership information is persisted in a database that gets created automatically. For more information about forms authentication in ASP.NET MVC, see Walkthrough: Using Forms Authentication in ASP.NET MVC.
成員(Membership)信息會被持久化在一個自動創建的數據庫中。關於ASP.NET MVC中表單認證的更多信息,請參閱“演練:在ASP.NET MVC中使用表單認證”。

Update the CSS File
更新CSS文件

This step is cosmetic, but it will make the pages render like the earlier screen shots.
這一步驟是裝飾性的,但它會使頁面渲染成前述的屏幕截圖。

In Solution Explorer, expand the Content folder and open the file named Site.css. Add the following CSS styles:
在“解決方案資源管理器”中展開“Content”文件夾,並打開名為Site.css的文件。添加以下CSS樣式:

.content { 
    clear: both; 
    width: 90%; 
} 
li { 
    list-style-type: none; 
} 
#products li { 
    width: 300px; 
    background-color: #aaf; 
    font-size: 1.5em; 
    font-weight: bold; 
    color: #ff0; 
    margin: 0 0 5px 0; 
    padding: 0 5px 0 5px; 
} 
.price  { 
    float: right; 
    color: #c00; 
    font-size: 0.75em; 
} 
.details thead td { 
    background-color: #CCCCCC; 
    color: #333333; 
} 
.details td { 
        padding: 6px; 
} 
.details td.qty { 
        text-align: center; 
}
#cart a { 
        color: Blue; 
        font-size: .75em; 
}  
#update-products li {  
    padding: 5px;  
    color: #666; 
    border-style: dashed; 
    border-width: 2px; 
    border-color: #666; 
} 
#update-products li .item { 
        width: 120px; 
        display: inline-block; 
        text-align: right; 
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM