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(1)/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(1)/$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(1)/$links/Orders(1)

这里展示的则是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: 50

请求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

 
 

Working with Entity Relations in OData的更多相关文章

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

    前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html. 本文的示例代码的下载地址 ...

  2. [转]Upgrading to Async with Entity Framework, MVC, OData AsyncEntitySetController, Kendo UI, Glimpse & Generic Unit of Work Repository Framework v2.0

    本文转自:http://www.tuicool.com/articles/BBVr6z Thanks to everyone for allowing us to give back to the . ...

  3. [转]Creating an OData v3 Endpoint with Web API 2

    本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata- ...

  4. Asp.Net Web API 2

    Asp.Net Web API 2第十八课——Working with Entity Relations in OData   前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导 ...

  5. Web API 2

    Asp.Net Web API 2 官网菜鸟学习系列导航[持续更新中]   前言 本来一直参见于微软官网进行学习的, 官网网址http://www.asp.net/web-api.出于自己想锻炼一下学 ...

  6. 杂项:ASP.NET Web API

    ylbtech-杂项:ASP.NET Web API ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动设备)的 HTTP 服务. ASP.NET Web A ...

  7. [转]Calling an OData Service From a .NET Client (C#)

    本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata- ...

  8. [转]ASP.NET web API 2 OData enhancements

    本文转自:https://www.pluralsight.com/blog/tutorials/asp-net-web-api-2-odata-enhancements Along with the ...

  9. [转]Using $select, $expand, and $value in ASP.NET Web API 2 OData

    本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/using- ...

随机推荐

  1. SQL Server使用规范

    原文:SQL Server使用规范 常见的字段类型选择 1.字符类型建议采用varchar/nvarchar数据类型 2.金额货币建议采用money数据类型 3.科学计数建议采用numeric数据类型 ...

  2. 从客户端(FCKeditor1="<p>...")中检测到有潜在危险的 Request.Form 值。

    从客户端(FCKeditor1="<p>...")中检测到有潜在危险的 Request.Form 值. 说明: 请求验证过程检测到有潜在危险的客户端输入值,对请求的处理 ...

  3. HDU4828 Grids 2014百度之星预赛问题解决

    看看Catalan数的公式:为 Catalan(n) = C(2n, n) / n+1 = C(2n, n) - C(2n, n-1); (公式0) 然后利用全排序表达:Catalan(n) = (2 ...

  4. crawler_wireshark 过滤基础知识

    一.IP过滤:包括来源IP或者目标IP等于某个IP比如:ip.src addr==192.168.0.208  or ip.src addr eq 192.168.0.208 显示来源IP       ...

  5. csdn 博客,你很努力,有人帮你-2015年03一个月17日本

    今天泛化 开始使用简书 正则表达式的博客写了两篇文章 回顾 Core Data 基本使用 总结 Xcode6新特性 简单聊聊 简书,事实上一開始学 MarkDown 语法的时候,已经用了,但是,一直认 ...

  6. measureChildren作品

    无论是在改写View依然是ViewGroup什么时候.特别ViewGrop什么时候,通常是不可避免的重写onMeasure方法,我们一定会调用setMeasuredDimension()将測量好的宽高 ...

  7. C语言练手自己编写学生成绩管理系统

    #include<stdio.h> #include<stdlib.h> /*定义学生结构体*/ struct Student { ]; ]; float Mark1; flo ...

  8. zoj 3822 Domination(dp)

    题目链接:zoj 3822 Domination 题目大意:给定一个N∗M的棋盘,每次任选一个位置放置一枚棋子,直到每行每列上都至少有一枚棋子,问放置棋子个数的期望. 解题思路:大白书上概率那一张有一 ...

  9. unpivot,pivot,case when ,行转列,列转行 sql server

    http://technet.microsoft.com/zh-cn/library/ms177410.aspx unpivot : CREATE TABLE pvt (VendorID int, E ...

  10. 尚学堂Java面试题整理

    博客分类: 经典分享   1. super()与this()的差别? - 6 -  2. 作用域public,protected,private,以及不写时的差别? - 6 -  3. 编程输出例如以 ...