原文:【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):

图2-8. ProductStore应用程序的浏览界面

注:作者对此应用程序作了简化,把一个在线商店常规的查看功能(产品列表、购物车、订单条目、订单细节)全部显示在一个Web页面上 — 译者注

Administrators can create, delete, or edit products:

管理员可以创建、删除,或编辑产品(见图2-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所示(译者注):

图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)。

图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)。

图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)。

图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;
}

【ASP.NET Web API教程】2.3 与实体框架一起使用Web API的更多相关文章

  1. 【ASP.NET Web API教程】3.2 通过.NET客户端调用Web API(C#)

    原文:[ASP.NET Web API教程]3.2 通过.NET客户端调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...

  2. EntityFramework_MVC4中EF5 新手入门教程之一 ---1.创建实体框架数据模型

    Contoso University  Web 应用程序 你会在这些教程中构建的应用程序是一个简单的大学网站. 用户可以查看和更新学生. 课程和教师信息.这里有几个屏幕,您将创建. 这个网站的用户界面 ...

  3. 【ASP.NET Web API教程】2 创建各种Web API

    原文 [ASP.NET Web API教程]2 创建各种Web API Chapter 2: Creating Web APIs第2章 创建各种Web API 本文引自:http://www.asp. ...

  4. 从实体框架核心开始:构建一个ASP。NET Core应用程序与Web API和代码优先开发

    下载StudentApplication.Web.zip - 599.5 KB 下载StudentApplication.API.zip - 11.5 KB 介绍 在上一篇文章中,我们了解了实体框架的 ...

  5. IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习保护API

    IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习之保护API. 使用IdentityServer4 来实现使用客户端凭据保护ASP.N ...

  6. 【ASP.NET Web API教程】6.4 模型验证

    本文是Web API系列教程的第6.4小节 6.4 Model Validation 6.4 模型验证 摘自:http://www.asp.net/web-api/overview/formats-a ...

  7. ASP.NET 5系列教程 (六): 在 MVC6 中创建 Web API

    ASP.NET 5.0 的主要目标之一是统一MVC 和 Web API 框架应用. 接下来几篇文章中您会了解以下内容: ASP.NET MVC 6 中创建简单的web API. 如何从空的项目模板中启 ...

  8. 【ASP.NET Web API教程】6.1 媒体格式化器

    http://www.cnblogs.com/r01cn/archive/2013/05/17/3083400.html 6.1 Media Formatters6.1 媒体格式化器 本文引自:htt ...

  9. 【ASP.NET Web API教程】1.1 第一个ASP.NET Web API

    Your First ASP.NET Web API (C#)第一个ASP.NET Web API(C#) By Mike Wasson|January 21, 2012作者:Mike Wasson ...

随机推荐

  1. 利用 html的锚点(元素a)功能实现ajax单页面应用的浏览器后退前进功能

    一.问题 随着AJax技术的普及,单页面web程序的应用越来越广泛. 所谓单页面应用程序,简单的说,就是应用只有一个主网页,第一次加载后,后续页面只会利用js和ajax到服务器获取数据进行页面的局部刷 ...

  2. 进入MFC讲坛的前言(三)

    MFC中的窗口创建及窗口消息映射 我经常碰到有人问我有关窗口创建的问题,他们经常把用HWND描述的系统窗口对象和用CWnd描述的MFC的窗口对象混淆不清.这两者之间是紧密联系在一起的,但是MFC为了自 ...

  3. Java之GUI编程(一)

    GUI全称Graphical User Interfaces,意为图形用户户界面,又称为图形用户接口.GUI指的就是採用图形方式显示的计算机操作用户界面,打个例如吧.我们点击QQ图标,就会弹出一个QQ ...

  4. stm32之watchdog

    在嵌入式系统中,由于MCU的工作常常受到来自外界电磁场的干扰,造成程序的跑飞,而陷入死循环,程序的正常运行被打断,由单片机控制的系统无法继续工作,会造成整个系统陷入停滞状态,发送不可预料的后果,所以出 ...

  5. linux cmd

    Linux查找包含特定字符串的文件名的方法 Pasted from <http://www.jbxue.com/LINUXjishu/9790.html> find .|xargs gre ...

  6. Security:蠕虫的行为特征描述和工作原理分析

    ________________________ 参考: 百度文库---蠕虫的行为特征描述和工作原理分析 http://wenku.baidu.com/link?url=ygP1SaVE4t4-5fi ...

  7. arm汇编:ldr,str,ldm,stm,伪指令ldr

    ldr,str,ldm,stm的命名规律: 这几个指令命名看起来不易记住,现在找找规律. 指令 样本 效果 归纳名称解释 ldr Rd,addressing ldr r1,[r0] addressin ...

  8. Arduino 入门程序示例之一个 LED(2015-06-11)

    前言 答应了群主写一些示例程序,一直拖延拖延拖延唉.主要还是害怕在各大高手面前班门弄斧……(这也算是给拖延症找一个美好的理由吧),这几天终于下决心要写出来了,各位高手拍砖敬请轻拍啊. 示例程序 首先是 ...

  9. cocos2d-x on wp8架构简单介绍

    1,基于C++的开发架构 支持3大移动平台以及3大桌面平台. 分为图形,声音,物理3大模块,另外还有脚本的导出. 在wp8/win32上的图形是基于d3d的,而在其它平台是基于opengl/openg ...

  10. Swift Error fatal error: unexpectedly found nil while unwrapping an Optional value

    致命的错误: 对一个为空的optional值进行解包. 强制解包(forced unwrapping): 在optional后面加叹号. 比如  "value!" 错误代码: le ...