Asp.Net Web API 2第十八课——Working with Entity Relations in OData

 

前言

  阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html。 

  本文的示例代码的下载地址为http://pan.baidu.com/s/1o6lqXN8

大多数的数据集定义实体间的关系:客户有订单、书籍有作者、产品有供应商。客户端可以使用OData操作实体间的关系。给定一个产品,你可以找到该产品的供应商。您也可以创建或者删除关系。例如,您也可以为一个产品设置一个供应商。

  本教程将会展示在Asp.Net Web API中支持这些操作。本文的教程是建立在上一节的教程之上http://www.cnblogs.com/aehyok/p/3545824.html

Add a Supplier Entity添加一个供应商实体类

首先我们需要来添加一个Supplier的实体类

namespace OData.Models
{
public class Supplier
{
[Key]
public string Key { get; set; }
public string Name { get; set; }
}
}

这个类使用了一个字符串类型的实体键。在实践中,这可能比使用整形键不太常见的。但它是值得的看到OData如何处理除了整数以外的其他键类型。

接下来,我们将通过在Product类上添加一个Supplier的属性来建立一个关系。

    public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
// New code
[ForeignKey("Supplier")]
public string SupplierId { get; set; }
public virtual Supplier Supplier { get; set; }
}

添加一个新的DbSetProductServiceContext类,从而使实体框架将包括Supplier在数据库表中。

    public class ProductServiceContext : DbContext
{
public ProductServiceContext() : base("name=ProductServiceContext")
{
} public DbSet<Product> Products { get; set; } ///New Code
public DbSet<Supplier> Suppliers { get; set; } }

在WebApiConfig.cs,添加一个“Suppliers”实体的EDM模型:

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Products");
// New code:
builder.EntitySet<Supplier>("Suppliers");

Navigation Properties导航属性

为了得到一个产品的供应商,客户端发送了一个Get请求:

GET /Products()/Supplier

在Product类型上有一个Supplier的导航属性。在这个实例中,Supplier是一个单一的项。但是一个导航属性也能返回一个集合(一对多或者多对多的 关系)。

为了支持这个请求,在ProductsController上添加如下方法:

        // GET /Products(1)/Supplier
public Supplier GetSupplier([FromODataUri] int key)
{
Product product = db.Products.FirstOrDefault(p => p.ID == key);
if (product == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return product.Supplier;
}

key这个参数就是这个Product的键。这个方法返回关联的实体——在这个实例中,就是一个Supplier对象。方法的名称和参数的名称都是非常重要的。总之,如果导航属性被命名为一个“X”,你需要添加一个被命名为“GetX”的方法。这个方法必须采用一个命名为“key”的参数,用来匹配父类数据类型的key。

它也是很重要的在键参数上拥有【FromOdataUri】的属性。当它从请求的URL中解析键时,这个属性将会告诉Web API去使用Odata语法规则。

Creating and Deleting Links

OData支持创建和删除两个实体之间的关系。在OData术语中,这个关系就是一个“link”。每个link有一个携带entity/$links/entity的Url。例如,由产品到供应商的链接看起来像这样:

/Products()/$links/Supplier

为了创建一个新的链接,这个客户端发送了一个post请求到这个链接URI。请求的消息体就是目标实体的URI。例如,假设有一个供应商的键为“CTSO”。为了创建一个链接由“Product(1)”到”Supplier('CTSO')“,客户端发送一个请求如下:

POST http://localhost/odata/Products(1)/$links/Supplier
Content-Type: application/json
Content-Length: 50
{"url":"http://localhost/odata/Suppliers('CTSO')"}

对于删除一个链接,客户端发送了一个DELETE 请求到链接URI。

Creating Links

为启用一个客户端去创建产品-供应商的链接,需要在ProductsController类中添加如下的代码:

[AcceptVerbs("POST", "PUT")]
public async Task<IHttpActionResult> CreateLink([FromODataUri] int key, string navigationProperty, [FromBody] Uri link)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
} Product product = await db.Products.FindAsync(key);
if (product == null)
{
return NotFound();
} switch (navigationProperty)
{
case "Supplier":
string supplierKey = GetKeyFromLinkUri<string>(link);
Supplier supplier = await db.Suppliers.FindAsync(supplierKey);
if (supplier == null)
{
return NotFound();
}
product.Supplier = supplier;
await db.SaveChangesAsync();
return StatusCode(HttpStatusCode.NoContent); default:
return NotFound();
}
}

这个方法有三个参数:

第一个key:就是引导到父类实体的键

第二个navigationProperty: 导航属性的名称。例如,最合适的导航属性Supplier。

第三个link:被链接实体的OData的URI。这个值是从消息体中获得。例如,这个链接URI可能是”http://localhost/odata/Suppliers('CTSO')“,也就是供应商中有ID="CTSO"。

这个方法用这个链接去查找Supplier。如果匹配的供应商被发现,这个方法将会设置Product实体类的Supplier的属性,并且保存结果到数据库。

其中最难的部分是解析链接URI。从根本上来说,你需要模拟发送一个get请求到那个URI。接下来的辅助方法将会展示如何处理它。这个方法调用Web API路由过程,返回一个OData实体,展现被转换的OData路径。对于一个链接URI,这个片段数中应该有一个实体键。

// Helper method to extract the key from an OData link URI.
private TKey GetKeyFromLinkUri<TKey>(Uri link)
{
TKey key = default(TKey); // Get the route that was used for this request.
IHttpRoute route = Request.GetRouteData().Route; // Create an equivalent self-hosted route.
IHttpRoute newRoute = new HttpRoute(route.RouteTemplate,
new HttpRouteValueDictionary(route.Defaults),
new HttpRouteValueDictionary(route.Constraints),
new HttpRouteValueDictionary(route.DataTokens), route.Handler); // Create a fake GET request for the link URI.
var tmpRequest = new HttpRequestMessage(HttpMethod.Get, link); // Send this request through the routing process.
var routeData = newRoute.GetRouteData(
Request.GetConfiguration().VirtualPathRoot, tmpRequest); // If the GET request matches the route, use the path segments to find the key.
if (routeData != null)
{
ODataPath path = tmpRequest.GetODataPath();
var segment = path.Segments.OfType<KeyValuePathSegment>().FirstOrDefault();
if (segment != null)
{
// Convert the segment into the key type.
key = (TKey)ODataUriUtils.ConvertFromUriLiteral(
segment.Value, ODataVersion.V3);
}
}
return key;
}

Deleting Links

对于删除一个链接,在ProductsController类中添加如下代码:

public async Task<IHttpActionResult> DeleteLink([FromODataUri] int key, string navigationProperty)
{
Product product = await db.Products.FindAsync(key);
if (product == null)
{
return NotFound();
} switch (navigationProperty)
{
case "Supplier":
product.Supplier = null;
await db.SaveChangesAsync();
return StatusCode(HttpStatusCode.NoContent); default:
return NotFound(); }
}

在这个例子中,这个导航属性是一个简单的Supplier实体。如果导航属性是一个集合,对于删除一个链接的URI必须在被关联的实体中有一个键。例如:

DELETE /odata/Customers()/$links/Orders()

这里展示的则是1对多的关系中,删除其中的一个的例子。

这个请求就是从客户1中移除订单为1的。这个DeleteLink方法将会有如下签名:

void DeleteLink([FromODataUri] int key, string relatedKey, string navigationProperty);

简单测试结果

1、http://localhost:3629/Odata/Products(1)/Supplier

2、

将ID=2的Supplier修改为WING

请求Header

POST http://localhost/odata/Products(2)/$links/Supplier
Content-Type: application/json
Content-Length:

请求Body

{"url":"http://localhost/odata/Suppliers('WING')"}

现在再次查看http://localhost/Odata/Products

3、DELETE  http://localhost/odata/Products(2)/$links/Supplier那么这样就可以将上面的SupplierId=WING修改为null

然后再次执行http://localhost/Odata/Products查看

总结

本文所参考链接为http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/working-with-entity-relations

本文示例代码下载地址为http://pan.baidu.com/s/1o6lqXN8

Asp.Net Web API 2的更多相关文章

  1. 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用

    由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...

  2. ASP.NET Web API Model-ActionBinding

    ASP.NET Web API Model-ActionBinding 前言 前面的几个篇幅把Model部分的知识点划分成一个个的模块来讲解,而在控制器执行过程中分为好多个过程,对于控制器执行过程(一 ...

  3. ASP.NET Web API Model-ParameterBinding

    ASP.NET Web API Model-ParameterBinding 前言 通过上个篇幅的学习了解Model绑定的基础知识,然而在ASP.NET Web API中Model绑定功能模块并不是被 ...

  4. ASP.NET Web API Model-ModelBinder

    ASP.NET Web API Model-ModelBinder 前言 本篇中会为大家介绍在ASP.NET Web API中ModelBinder的绑定原理以及涉及到的一些对象模型,还有简单的Mod ...

  5. ASP.NET Web API Model-ValueProvider

    ASP.NET Web API Model-ValueProvider 前言 前面一篇讲解了Model元数据,Model元数据是在Model绑定中很重要的一部分,只是Model绑定中涉及的知识点比较多 ...

  6. ASP.NET Web API Model-ModelMetadata

    ASP.NET Web API Model-ModelMetadata 前言 前面的几个篇幅主要围绕控制器的执行过程,奈何执行过程中包含的知识点太庞大了,只能一部分一部分的去讲解,在上两篇中我们看到在 ...

  7. ASP.NET Web API 过滤器创建、执行过程(二)

    ASP.NET Web API 过滤器创建.执行过程(二) 前言 前面一篇中讲解了过滤器执行之前的创建,通过实现IFilterProvider注册到当前的HttpConfiguration里的服务容器 ...

  8. ASP.NET Web API 过滤器创建、执行过程(一)

    ASP.NET Web API 过滤器创建.执行过程(一) 前言 在上一篇中我们讲到控制器的执行过程系列,这个系列要搁置一段时间了,因为在控制器执行的过程中包含的信息都是要单独的用一个系列来描述的,就 ...

  9. ASP.NET Web API 控制器执行过程(一)

    ASP.NET Web API 控制器执行过程(一) 前言 前面两篇讲解了控制器的创建过程,只是从框架源码的角度去简单的了解,在控制器创建过后所执行的过程也是尤为重要的,本篇就来简单的说明一下控制器在 ...

  10. ASP.NET Web API 控制器创建过程(二)

    ASP.NET Web API 控制器创建过程(二) 前言 本来这篇随笔应该是在上周就该写出来发布的,由于身体跟不上节奏感冒发烧有心无力,这种天气感冒发烧生不如死,也真正的体会到了什么叫病来如山倒,病 ...

随机推荐

  1. where 1=1

    sql: where 1=1 1=1 永真, 1<>1 永假. 1<>1 的用处: 用于只取结构不取数据的场合 例如: 拷贝表 create   table_name   as ...

  2. Riak VClock

    Riak VClock 关于向量时钟的概念.在这里就多讲了,大家能够參照一下Dynamo的论文了解一下,向量时钟在分布式主要用于解决一致性性问题.能够和CRDTs一起看. 以下的源码是參照riak中的 ...

  3. Ansible@一个有效的配置管理工具--Ansible configure management--翻译(十二)

    如果没有书面授权,请勿转载 第五章 自己定义模块 External inventories In the first chapter we saw how Ansible needs an inven ...

  4. SSH框架-Caused by: org.hibernate.MappingException: column attribute may not be used together with <column> subelement

    昨晚修改了一些表关系,在相关的hbm.xml文件中做了改动,今天早上起来启动tomcat后,发现项目启动不了,控制台报错: 2015-6-14 9:09:42 org.apache.catalina. ...

  5. php错误日志级别

    ; E_ALL             所有错误和警告(除E_STRICT外) ; E_ERROR           致命的错误.脚本的执行被暂停. ; E_RECOVERABLE_ERROR   ...

  6. mysql源码安装(5.1)

    下载mysql源码包并解压.wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.73.tar.gztar -zxvf mysql-5 ...

  7. jsp页面使用jstl标签格式化String类型日期

    1.引入jstl <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> ...

  8. android——ObjectAnimator动画(一)

    直接贴上集中用法 package com.example.test; import com.example.test.views.CircleView; import android.animatio ...

  9. android XML解析之DOM解析方式

    DOM 解析方式步骤: 第一步:首选需要获得DOM解析器工厂实例 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ...

  10. win7系统64位安装oracle10g

    win7系统64位安装oracle10g 下载地址: http://download.oracle.com/otn/nt/oracle10g/10204/10204_vista_w2k8_x64_pr ...