数据服务快速入门(ADO.NET 数据服务框架)
按照本主题中描述的步骤执行操作,您将使用 ADO.NET 数据服务创建和测试连接到 Northwind 示例数据库的数据服务。
若要创建此示例数据服务,请先确保计算机上已安装本主题的“系统必备”一节中列出的必需组件。若要创建此数据服务,请按照以下过程执行操作:
创建 ASP.NET Web 应用程序。
创建实体数据模型 (EDM) 并将此模型连接到数据库。
在 Web 应用程序中创建数据服务。
启用对数据服务的访问。
通过执行上述步骤创建并启用数据服务后,可以尝试已实现的示例数据服务。
系统必备
若要使用 ADO.NET 数据服务框架创建数据服务,则必须在计算机上安装下列组件:
ADO.NET 数据服务和 ADO.NET 实体框架。在安装 Microsoft .NET Framework 3.5 Service Pack 1 (SP1) 或更高版本时,这两个组件会随之安装。
ADO.NET 数据服务框架包含运行时组件和数据访问组件。运行时组件可实现 URI 转换、AtomPub 和 JSON 传输格式以及交互协议。数据访问组件通过 IQueryable<T> 接口支持组件之间的通信。一组约定将公共语言运行库 (CLR) 类型映射到 ADO.NET 数据服务使用的 URI 语法和负载协议。
实体框架工具。Visual Studio 2008 Service Pack 1 (SP1) 中包含这些工具。
ADO.NET 数据访问提供程序。任何版本的 Microsoft SQL Server 2005 都支持这些提供程序,其中包括 SQL Server Express。
Northwind 示例数据库。若要下载此示例数据库,请访问 SQL Server 的示例数据库的下载页。
创建 ASP.NET Web 应用程序
ADO.NET 数据服务是 Windows Communication Foundation 服务的专用版本。ADO.NET 数据服务在 ASP.NET 站点中运行。
如果已有一个 ASP.NET Web 应用程序并希望将示例数据服务添加到此现有应用程序,则可以跳过下列步骤,并转到下一节“创建实体数据模型”。
注意 |
---|
以下过程使用了 Standard、Professional 和 Team System 版本的 Visual Studio 2008 SP1。如果使用的是 Visual Studio Web Developer,则必须创建新的网站,而不是新的 Web 应用程序。 |
在 Visual Studio 中的**“文件”菜单上,选择“新建”,然后选择“项目”**。
在**“新建项目”**对话框中,选择 Visual Basic 或 Visual C# 作为编程语言。
在**“模板”窗格中,选择“ASP.NET Web 应用程序”**。
键入项目的名称,如 SimpleDataService。
单击**“确定”**。
创建实体数据模型
若要使用 ADO.NET 实体框架创建基于关系数据库的 EDM,请执行下列操作:
在解决方案资源管理器中,右键单击刚创建的 ASP.NET 项目名称,然后单击**“添加新项”**。
在**“添加新项”对话框中,选择“ADO.NET 实体数据模型”**。
键入 Northwind 作为数据模型的名称。
Northwind.edmx 是通过实体数据模型向导创建的数据模型的文件名。所创建的数据模型的架构与 Northwind 示例数据库之间将建立一对一的映射关系。
在实体数据模型向导中,选择**“从数据库生成”,然后单击“下一步”**。
通过执行下列步骤之一,将数据模型连接到数据库,然后单击**“下一步”**:
如果尚未配置数据库连接,请单击**“新建连接”**并创建一个新连接。
注意 如果使用的是 SQL Server 2005,建议您将“MultipleActiveResultSets”选项设为 true。如果此选项在连接字符串中未设置为 true,请在“连接属性”对话框中,单击“高级”,然后将“MultipleActiveResultSets”选项设置为 true。
- 或 -
如果已配置一个连接到 Northwind 数据库的数据库连接,请从连接列表中选择该连接。
在向导的最后一页中,选中数据库中所有表对应的复选框,并清除视图和存储过程对应的复选框。
向导的最后一页会显示一个包含数据库中提供的表、视图和存储过程的列表。若要保留本主题示例中创建的数据模型,请仅选择表。
单击**“完成”**关闭向导。
此向导将生成多个表示数据库元数据的文件,并将这些文件添加到项目中。
创建数据服务
若创建 ADO.NET 数据服务,则会将在前面的步骤中创建的数据模型部署为可通过 Internet 协议访问的服务。
若要创建数据服务,请执行下列操作:
在解决方案资源管理器中,右键单击 ASP.NET 项目的名称,然后单击**“添加新项”**。
在**“添加新项”对话框中,选择“ADO.NET 数据服务”**。
注意 请务必选择“ADO.NET 数据服务”,而不是其他类似选项,如“Web 服务”。
键入 Northwind 作为服务的名称。
Visual Studio 将为新服务创建 XML 标记和代码文件。默认情况下,代码编辑器窗口将打开。在解决方案资源管理器中,该服务的名称为 Northwind 并带有扩展名 .svc.cs 或 .svc.vb。
通过执行下列操作,在数据服务的代码开头包含实体数据模型向导生成的数据模型类的命名空间:
如果 ASP.NET 应用程序使用的是 Visual Basic,请添加一个
imports
语句。如果 ASP.NET 应用程序使用的是 C#,请添加一个
using
语句。
Visual Studio 对象浏览器将显示派生自数据服务使用的数据库的命名空间。在此示例中,数据库的名称为 Northwind,命名空间的名称为 NorthwindModel。
在数据服务的代码中,找到“TODO”注释,并将此注释替换为实体数据模型向导生成的类的名称。
在此示例中,实体数据模型向导生成的类的名称为 NorthwindModel 命名空间中的 NorthwindEntities。
启用对数据服务的访问
默认情况下,数据服务不启用对任何资源的访问。必须先显式启用访问,然后数据服务才能返回数据。
若要启用对与服务关联的所有资源的读写访问,请在派生自 DataService 的类中找到 InitializeService
函数。向此函数中添加将容器的资源权限设置为“全部”时所必需的代码。在 C# 中,向 InitializeService
函数中添加行 config.SetResourceContainerAccessRule("*", ResourceContainerRights.All)
,如下面的代码所示:
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using NorthwindModel;
namespace SimpleDataService
{
public class Northwind : DataService< NorthwindModel.NorthwindEntities >
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(IDataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and
// service operations are visible and updatable.
// Use *(all sets) only for testing.
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
}
}
}
现在,可以从 Visual Studio 中运行服务。按 F5 启动开发 Web 服务器。下一节将介绍如何使用新创建的数据服务。
尝试数据服务
为了实现此示例的目的,将使用 Web 浏览器与数据服务进行交互。利用 Web 浏览器,可以轻松试验请求的寻址语法并查看结果。不过,在数据服务已完成和正在使用时,大多数应用程序都不会使用 Web 浏览器与数据服务进行交互。通常,应用程序会通过代码或脚本语言与数据服务进行交互。
注意 |
---|
有关 ADO.NET 数据服务使用的 HTTP 语法的更多信息,请参见使用统一的 URI 对数据进行寻址的简单方案(ADO.NET 数据服务框架)和 ADO.NET 数据服务的客户端应用程序。 |
检索默认服务文档
默认情况下,数据服务返回的 XML 文档是一个 AtomPub 服务文档。AtomPub 是默认的序列化方法。有关 ATOM 协议的更多信息,请访问 ADO.NET 数据服务团队网站。
请求默认服务文档:
打开 Web 浏览器,然后输入一个与下面的 URI 类似的 URI,并替换与您的开发环境匹配的端口号:
https://localhost:50781/northwind.svc
如果已根据前一个 URI 设置访问规则,则数据服务会将默认服务文档返回到浏览器。此默认服务文档包含一个实体集的列表,这些实体集表示数据服务部署的 Northwind 数据。此默认服务文档将与下面的 XML 示例类似:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<service xml:base="https://localhost:1365/Northwind.svc/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app">
<workspace>
<atom:title>Default</atom:title>
<collection href="Categories">
<atom:title>Categories</atom:title>
</collection>
<collection href="CustomerDemographics">
<atom:title>CustomerDemographics</atom:title>
</collection>
<collection href="Customers">
<atom:title>Customers</atom:title>
</collection>
<collection href="Employees">
<atom:title>Employees</atom:title>
</collection>
<collection href="Order_Details">
<atom:title>Order_Details</atom:title>
</collection>
<collection href="Orders">
<atom:title>Orders</atom:title>
</collection>
<collection href="Products">
<atom:title>Products</atom:title>
</collection>
<collection href="Region">
<atom:title>Region</atom:title>
</collection>
<collection href="Shippers">
<atom:title>Shippers</atom:title>
</collection>
<collection href="Suppliers">
<atom:title>Suppliers</atom:title>
</collection>
<collection href="Territories">
<atom:title>Territories</atom:title>
</collection>
</workspace>
</service>
检索实体集
若要浏览数据服务的内容,请将默认服务文档中的实体集用作起始点。例如,示例数据服务的默认服务文档中的一些实体集包括 Products
、Shippers
和 Territories
。可以轻松返回这些实体集之一中的所有实体。
请求产品列表
在 Web 浏览器中,输入以下 URI:
https://localhost/northwind.svc/Products
向 URI 添加 /Products
后,此 URI 将返回所有存货产品。下面的 XML 显示返回的前四个产品,这是结果的部分列表:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="https://localhost:1365/Northwind.svc/" xmlns:d="https://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="https://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Products</title>
<id>https://localhost:1365/Northwind.svc/Products</id>
<updated>2008-05-22T16:42:14Z</updated>
<link rel="self" title="Products" href="Products" />
<entry m:type="NorthwindModel.Products">
<id>https://localhost:1365/Northwind.svc/Products(1)</id>
<title type="text"></title>
<updated>2008-05-22T16:42:14Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Products" href="Products(1)" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories"
type="application/atom+xml;type=entry" title="Categories" href="Products(1)/Categories" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Order_Details"
type="application/atom+xml;type=feed" title="Order_Details" href="Products(1)/Order_Details" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Suppliers"
type="application/atom+xml;type=entry" title="Suppliers" href="Products(1)/Suppliers" />
<content type="application/xml">
<m:properties>
<d:Discontinued m:type="Edm.Boolean">false</d:Discontinued>
<d:ProductID m:type="Edm.Int32">1</d:ProductID>
<d:ProductName>Chai</d:ProductName>
<d:QuantityPerUnit>10 boxes x 20 bags</d:QuantityPerUnit>
<d:ReorderLevel m:type="Edm.Int16">10</d:ReorderLevel>
<d:UnitPrice m:type="Edm.Decimal">18.0000</d:UnitPrice>
<d:UnitsInStock m:type="Edm.Int16">36</d:UnitsInStock>
<d:UnitsOnOrder m:type="Edm.Int16">0</d:UnitsOnOrder>
</m:properties>
</content>
</entry>
<entry m:type="NorthwindModel.Products">
<id>https://localhost:1365/Northwind.svc/Products(2)</id>
<title type="text"></title>
<updated>2008-05-22T16:42:14Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Products" href="Products(2)" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories"
type="application/atom+xml;type=entry" title="Categories" href="Products(2)/Categories" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Order_Details"
type="application/atom+xml;type=feed" title="Order_Details" href="Products(2)/Order_Details" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Suppliers"
type="application/atom+xml;type=entry" title="Suppliers" href="Products(2)/Suppliers" />
<content type="application/xml">
<m:properties>
<d:Discontinued m:type="Edm.Boolean">false</d:Discontinued>
<d:ProductID m:type="Edm.Int32">2</d:ProductID>
<d:ProductName>Chang</d:ProductName>
<d:QuantityPerUnit>24 - 12 oz bottles</d:QuantityPerUnit>
<d:ReorderLevel m:type="Edm.Int16">25</d:ReorderLevel>
<d:UnitPrice m:type="Edm.Decimal">19.0000</d:UnitPrice>
<d:UnitsInStock m:type="Edm.Int16">28</d:UnitsInStock>
<d:UnitsOnOrder m:type="Edm.Int16">40</d:UnitsOnOrder>
</m:properties>
</content>
</entry>
<entry m:type="NorthwindModel.Products">
<id>https://localhost:1365/Northwind.svc/Products(3)</id>
<title type="text"></title>
<updated>2008-05-22T16:42:14Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Products" href="Products(3)" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories"
type="application/atom+xml;type=entry" title="Categories" href="Products(3)/Categories" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Order_Details"
type="application/atom+xml;type=feed" title="Order_Details" href="Products(3)/Order_Details" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Suppliers"
type="application/atom+xml;type=entry" title="Suppliers" href="Products(3)/Suppliers" />
<content type="application/xml">
<m:properties>
<d:Discontinued m:type="Edm.Boolean">false</d:Discontinued>
<d:ProductID m:type="Edm.Int32">3</d:ProductID>
<d:ProductName>Aniseed Syrup</d:ProductName>
<d:QuantityPerUnit>12 - 550 ml bottles</d:QuantityPerUnit>
<d:ReorderLevel m:type="Edm.Int16">25</d:ReorderLevel>
<d:UnitPrice m:type="Edm.Decimal">10.0000</d:UnitPrice>
<d:UnitsInStock m:type="Edm.Int16">65</d:UnitsInStock>
<d:UnitsOnOrder m:type="Edm.Int16">70</d:UnitsOnOrder>
</m:properties>
</content>
</entry>
<entry m:type="NorthwindModel.Products">
<id>https://localhost:1365/Northwind.svc/Products(4)</id>
<title type="text"></title>
<updated>2008-05-22T16:42:14Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Products" href="Products(4)" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories"
type="application/atom+xml;type=entry" title="Categories" href="Products(4)/Categories" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Order_Details"
type="application/atom+xml;type=feed" title="Order_Details" href="Products(4)/Order_Details" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Suppliers"
type="application/atom+xml;type=entry" title="Suppliers" href="Products(4)/Suppliers" />
<content type="application/xml">
<m:properties>
<d:Discontinued m:type="Edm.Boolean">false</d:Discontinued>
<d:ProductID m:type="Edm.Int32">4</d:ProductID>
<d:ProductName>Chef Anton's Cajun Seasoning</d:ProductName>
<d:QuantityPerUnit>48 - 6 oz jars</d:QuantityPerUnit>
<d:ReorderLevel m:type="Edm.Int16">0</d:ReorderLevel>
<d:UnitPrice m:type="Edm.Decimal">22.0000</d:UnitPrice>
<d:UnitsInStock m:type="Edm.Int16">6</d:UnitsInStock>
<d:UnitsOnOrder m:type="Edm.Int16">0</d:UnitsOnOrder>
</m:properties>
</content>
</entry>
</feed>
检索单个实体
不仅可以返回一个实体集中的实体的列表,还可以请求特定实体。单个实体的 URI 依次包含以下几个部分:服务的基 URI、实体集的名称和实体的最终 ID。实体的 ID 是一个用括号括起的键值。
请求 ID 为 1 的产品:
在 Web 浏览器中,输入以下 URI:
http://host/vdir/northwind.svc/Products(1)
浏览器中刚输入的 URI 将检索 ID 为 1 的产品。下面的 XML 显示此请求的结果:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xml:base="https://localhost:1365/Northwind.svc/"
xmlns:d="https://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="https://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
m:type="NorthwindModel.Products" xmlns="http://www.w3.org/2005/Atom">
<id>https://localhost:1365/Northwind.svc/Products(1)</id>
<title type="text"></title>
<updated>2008-05-22T16:47:12Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Products" href="Products(1)" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories"
type="application/atom+xml;type=entry" title="Categories" href="Products(1)/Categories" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Order_Details"
type="application/atom+xml;type=feed" title="Order_Details" href="Products(1)/Order_Details" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Suppliers"
type="application/atom+xml;type=entry" title="Suppliers" href="Products(1)/Suppliers" />
<content type="application/xml">
<m:properties>
<d:Discontinued m:type="Edm.Boolean">false</d:Discontinued>
<d:ProductID m:type="Edm.Int32">1</d:ProductID>
<d:ProductName>Chai</d:ProductName>
<d:QuantityPerUnit>10 boxes x 20 bags</d:QuantityPerUnit>
<d:ReorderLevel m:type="Edm.Int16">10</d:ReorderLevel>
<d:UnitPrice m:type="Edm.Decimal">18.0000</d:UnitPrice>
<d:UnitsInStock m:type="Edm.Int16">36</d:UnitsInStock>
<d:UnitsOnOrder m:type="Edm.Int16">0</d:UnitsOnOrder>
</m:properties>
</content>
</entry>
检索实体关系
在此示例数据服务中,有一些值是标量,例如 ProductName
和 UnitPrice
。而另外一些值(例如 Categories
和 Suppliers
)表示导航属性的结尾。导航属性是一些快捷属性,用于定位通过 EDM 关联进行关联的实体。
例如,Products
实体包含与表示 Suppliers
和 Categories
的实体的关联。可以创建一个用于遍历这些关系的 URI。
请求 ID 为 2 的产品的供应商:
在 Web 浏览器中,输入以下 URI:
http://host/vdir/northwind.svc/Products(2)/Suppliers
下面的 XML 显示此请求的结果:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xml:base="https://localhost:1365/Northwind.svc/"
xmlns:d="https://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="https://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
m:type="NorthwindModel.Suppliers" xmlns="http://www.w3.org/2005/Atom">
<id>https://localhost:1365/Northwind.svc/Suppliers(1)</id>
<title type="text"></title>
<updated>2008-05-22T16:50:11Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Suppliers" href="Suppliers(1)" />
<link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Products"
type="application/atom+xml;type=feed" title="Products" href="Suppliers(1)/Products" />
<content type="application/xml">
<m:properties>
<d:Address>49 Gilbert St.</d:Address>
<d:City>London</d:City>
<d:CompanyName>Exotic Liquids</d:CompanyName>
<d:ContactName>Charlotte Cooper</d:ContactName>
<d:ContactTitle>Purchasing Manager</d:ContactTitle>
<d:Country>UK</d:Country>
<d:Fax m:null="true" />
<d:HomePage m:null="true" />
<d:Phone>(171) 555-2222</d:Phone>
<d:PostalCode>EC1 4SD</d:PostalCode>
<d:Region m:null="true" />
<d:SupplierID m:type="Edm.Int32">1</d:SupplierID>
</m:properties>
</content>
</entry>
另请参见
概念
用于对资源进行寻址的 URI 格式(ADO.NET 数据服务框架)
通用 HTTP 要求(ADO.NET 数据服务框架)