【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。

Download the completed project.
下载完整项目

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:
虽然这个教程是自包含的,但你也许想先阅读前面的两个教程:

  • Your First ASP.NET Web API
    第一个ASP.NET Web API
  • Creating a Web API that Supports CRUD Operations
    创建一个支持CRUD操作的Web API

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

转载于:https://www.cnblogs.com/r01cn/archive/2012/11/12/2767208.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/461589.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【原】webpack--文件监听的原理

轮询判断文件的最后编辑时间是否发生变化,一开始有个文件的修改时间,先存储起来这个修改时间,下次再有修改就会和上次修改时间比对,发现不一致的时候不会立即告诉监听者,而是把文件修改缓存起来,等待一段时…

ITTC数据挖掘平台介绍(综述)——平台简介

数据挖掘方兴未艾,大量新事物层出不穷。本系列将介绍我们自主设计的数据挖掘软件平台。与大家共同分享对知识,微博,人际等复杂网络的分析,以及对自然语言处理的见解。 一、我们需要怎样的数据挖掘系统 一直以来,以高校…

Linux—程序包安装与管理

1、软件包是对于一种软件所进行打包的方式。在不同的操作系统中,软件包的类型有很大的区别。对于Linux系统中,软件包主要以两种形式出现:二进制包以及源代码包。二进制包:1)传统的red hat linux二进制包2)d…

strlen()函数 与 sizeof运算符

以下内容源于网络资源的学习与整理,如有侵权请告知删除。欢迎交流。 一、strlen()函数 函数模型 size_t strlen(const char *s); 函数作用 strlen()函数是C库函数,其功能是计算字符串长度。 即从代表该字符串的第一个地址开始遍历,直到遇到‘…

Master-Detail(主表明细),确认可以出货的SQL指令 -- Not Exists

这是我文章的备份,原文请看: http://www.dotblogs.com.tw/mis2000lab/archive/2011/08/18/master_detail_finish_and_shipping.aspx [补充]下集,第十四章。Master-Detail(主表明细),确认可以出货的SQL指令…

开发板——X210开发板的SD卡启动方式

以下内容源于朱有鹏嵌入式课程的学习与整理,如有侵权请告知删除。 前言与总结 这里说的SD卡启动方式,指的是uboot在SD卡中或者在inand里,且启动介质拨码开关选择SD卡启动方式(对于X210,是拨到远离电源键的一侧&#xf…

使用iBATIS3.0完成增删改查

为什么80%的码农都做不了架构师?>>> 使用iBATIS3.0完成增删改查 iBATIS3.0和以前的版本有一些改变,不过学过以前版本的再学习3.0应该不是太难,3.0要求JDK1.5支持,因为其中增加了注解和泛型,这些都是JDK1.5…

oracle-11g-R2监听文件配置

客户端连接oracle数据库时出现如下错误: Listener refused the connection with the following error: ORA-12514, TNS:listener does not currently know of service requested in connect descriptor 首先看看Oracle服务是否开启: 然后找到listener.or…

【C】strcpy()需谨慎使用;

大家都知道C中的strcpy()函数是用来复制字符串的库函数。先附上代码看看strcpy()函数的功能&#xff1a; 1 #include<stdio.h>2 #include<string.h>3 #define MAX 204 5 int main(void)6 {7 char a[MAX]"abc";8 char b[MAX]"abcdefghi"…

多重指针操作

之前对多重指针操作心存忐忑&#xff0c;不能很熟练使用&#xff0c;本质原因是不了解其实质&#xff0c;因此对其进行了学习。 一、简单的代码如下 #include <stdio.h> #include <stdlib.h> #include <string.h>void myArray(char ***p2, int num) {int i0;…

C++ 初始化与赋值

1、初始化与赋值的区别&#xff1a; 二者的区别不是看&#xff0c;是否有这个赋值操作符&#xff0c;而是看操作的时候&#xff0c;对象是否已经有值。 初始化&#xff1a;创建对象&#xff0c;并给它设置初始值。 赋值&#xff1a;对象已经有值&#xff0c;擦除对象的当前值&a…

如何找出MySQL数据库中的低效SQL语句

面对业务的迅猛发展&#xff0c;DBA的一项重要工作就是及时发现数据库中的低效SQL语句&#xff0c;有的可以立刻着手解决&#xff08;比如缺少合适的索引&#xff09;&#xff0c;有的需要尽快反馈给开发人员进行修改。 MySQL数据库有几个配置选项可以帮助我们及时捕获低效SQL语…

搜集的一些有意思的牛人博客地址

我要去桂林-田春峰的IT网志 http://blog.donews.com/accesine/ SEO优化的内容 转载于:https://www.cnblogs.com/zhangweilong/archive/2012/11/19/2777719.html

memory拷贝与string拷贝的区别

1、memory拷贝&#xff0c;根据拷贝的字节个数&#xff0c;从src一个一个字节拷贝到dst&#xff0c;拷贝过程不管src的取值&#xff0c;也不管dst是否能容纳。2、因此&#xff0c;对于memory拷贝&#xff0c;src中NULL字符&#xff08;取值为0的字符&#xff09;后面的字符也能…

【转载】#323 - A Generic Class is a Template for a Class

A generic classs is a class that takes one or more type parameters, which it then uses in the definition of the class. It can be thought of as a template for a class. 1 public class ThingContainer<TParam> 2 { 3   private TParam theThing; 4 5   p…

黑马程序员--ADO.Net中的知识点和难点

------- Windows Phone 7手机开发、.Net培训、期待与您交流&#xff01; ------- 1、 ADO.Net基础 程序要和数据库交互要通过ADO.Net进行&#xff0c;通过ADO.Net就能在程序中执行SQL了。 直接在项目中内嵌mdf文件的方式使用SQLServer数据库&#xff08;基于服务的数据库&#…

leetcode——15.3Sum

#include<stdio.h> #include<stdlib.h>int** threeSum(int* nums, int numsSize, int* returnSize) {if (numsSize<3)return NULL;int comNum numsSize*(numsSize - 1)*(numsSize - 2) / 6;int** ret (int**)malloc(sizeof(int*)*comNum);//先创建“用来存储一…

[New Portal]Windows Azure Virtual Machine (17) Virtual Machine成本分析

《Windows Azure Platform 系列文章目录》 在Windows Azure VM里&#xff0c;计费模式是和以下几个因素有关&#xff1a; 成本1: VM Type and VM Size 具体的计费模式请参考这里 http://www.windowsazure.com/en-us/pricing/details/virtual-machines/#service-windows 我们知道…

Servlet与JSP间的传值问题

Servlet与JSP 之间的传值有两种情况&#xff1a;JSP -> Servlet&#xff0c; Servlet -> JSP。通过对象 request和 session (不考虑 application)完成传值。一、JSP -> servletJSP页面有3种方法向 servlet传值&#xff1a; form表单、URL<!-- JSP page --> ...…

java 访问修饰符

java 访问修饰符 类、方法、成员变量和局部变量的可用修饰符 修饰符 类 成员访求 构造方法 成员变量 局部变量 abstract&#xff08;抽象的&#xff09; √ √ &#xff0d; &#xff0d; &#xff0d; static &#xff08;静态的&#xff09; &#xff0d; √ …