通常情况下,一个OData的EDM(Entity Data Model)在配置的时候定义了,才可以被查询或执行各种操作。比如如下:

builder.EntitySet<SomeModel>("SomeModels");

可能会这样查询:http://localhost:8888/odata/SomeModels

如果SomeModel中有一个集合导航属性,该如何获取呢?比如:

public class SomeModel
{
public int Id{get;set;} public IList<AnotherModel> AnotherModels{get;set;}
} public class AnotherModel
{

我们是否可以直接在SomeModel中获取所有的AnotherModel, 而不是通过如下方式获取:

builder.EntitySet<AnotherModel>("AnotherModels");
http://localhost:8888/odata/AnotherModels

OData为我们提供了Containment,只要为某个集合导航属性加上[Contained]特性,就可以按如下方式获取某个EDM模型下的集合导航属性,比如:

http://localhost:8888/odata/SomeModels(1)/AnotherModels

好先定义模型。

public class Account
{
public int AccountID { get; set; }
public string Name { get; set; } [Contained]
public IList<PaymentInstrument> PayinPIs { get; set; }
} public class PaymentInstrument
{
public int PaymentInstrumentID { get; set; }
public string FriendlyName { get; set; }
}

以上,一旦在PayinPIs这个集合导航属性上加上[Contained]特性,只要在controller中再提供获取集合导航属性的方法,我们就可以按如下方式,通过Account获取PaymentInstrument集合。如下:

http://localhost:8888/odata/Accounts(100)/PayinPIs

在WebApiConfig类中定义如下:

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
... config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "odata",
model: GetModel());
} private static IEdmModel GetModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntityType<PaymentInstrument>();
builder.EntitySet<Account>("Accounts"); return builder.GetEdmModel();
}
}

在API端定义如下:

public class AccountsController : ODataController
{ private static IList<Account> _accounts = null; public AccountsController()
{
if(_accounts==null)
{
_accounts = InitAccounts();
}
} [EnableQuery]
public IHttpActionResult Get()
{
return Ok(_accounts.AsQueryable());
} [EnableQuery]
public IHttpActionResult GetById([FromODataUri] int key)
{
var account = _accounts.Single(a => a.AccountID == key);
return Ok(account);
} //获取Account所有的PaymentInstrument集合
[EnableQuery]
public IHttpActionResult GetPayinPIs([FromODataUri]int key)
{
var payinPIs = _accounts.Single(a => a.AccountID == key).PayinPIs;
return Ok(payinPIs);
} private static IList<Account> InitAccounts()
{
var accounts = new List<Account>()
{
new Account()
{
AccountID = ,
Name="Name100",
PayoutPI = new PaymentInstrument()
{
PaymentInstrumentID = ,
FriendlyName = "Payout PI: Paypal",
},
PayinPIs = new List<PaymentInstrument>()
{
new PaymentInstrument()
{
PaymentInstrumentID = ,
FriendlyName = "101 first PI",
},
new PaymentInstrument()
{
PaymentInstrumentID = ,
FriendlyName = "102 second PI",
},
},
},
};
return accounts;
}
}

以上的GetPayinPIs方法可以让我们根据Account获取其集合导航属性PayinPIs。

好,现在PayinPIs加上了[Contained]特性,也配备了具体的Action,现在开始查询:

http://localhost:64696/odata/Accounts(100)/PayinPIs

能查询到所有的PaymentInstrument。

此时,PayinPIs集合导航属性在元数据中是如何呈现的呢?查询如下:

http://localhost:64696/odata/$metadata

相关部分为:

<EntityType Name="Account">
<Key>
<PropertyRef Name="AccountID" />
</Key>
<Property Name="AccountID" Type="Edm.Int32" Nullable="false" />
<Property Name="Name" Type="Edm.String" />
<NavigationProperty Name="PayinPIs" Type="Collection(MyODataContainmentSample.PaymentInstrument)" ContainsTarget="true" />
</EntityType>

如果把PayinPIs上的[Contained]特性去掉呢?去掉后再次查询如下:

http://localhost:64696/odata/Accounts(100)/PayinPIs

返回404 NOT FOUND

再来看去掉[Contained]特性后相关的元数据:

<NavigationProperty Name="PayinPIs" Type="Collection(MyODataContainmentSample.PaymentInstrument)" />

没去掉[Contained]特性之前是:
<NavigationProperty Name="PayinPIs" Type="Collection(MyODataContainmentSample.PaymentInstrument)" ContainsTarget="true" />

原来,在一个集合导航属性上添加[Contained]特性,实际是让ContainsTarget="true",而默认状况下,ContainsTarget="false"。

^_^

在ASP.NET Web API中使用OData的Containment的更多相关文章

  1. ASP.NET Web API中使用OData

    在ASP.NET Web API中使用OData 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在ASP.NET Web API中,对于CRUD(creat ...

  2. 在ASP.NET Web API中使用OData

    http://www.alixixi.com/program/a/2015063094986.shtml 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在A ...

  3. 在ASP.NET Web API中使用OData的Action和Function

    本篇体验OData的Action和Function功能.上下文信息参考"ASP.NET Web API基于OData的增删改查,以及处理实体间关系".在本文之前,我存在的疑惑包括: ...

  4. [翻译]在ASP.NET Web API中通过OData支持查询和分页

    OData可以通过形如http://localhost/Products?$orderby=Name这样的QueryString传递查询条件.排序等.你可以在任何Web API Controller中 ...

  5. 在ASP.NET Web API中使用OData的单例模式

    从OData v4开始增加了对单例模式的支持,我们不用每次根据主键等来获取某个EDM,就像在C#中使用单例模式一样.实现方式大致需要两步: 1.在需要实现单例模式的导航属性上加上[Singleton] ...

  6. ASP.NET Web API中的Controller

    虽然通过Visual Studio向导在ASP.NET Web API项目中创建的 Controller类型默认派生与抽象类型ApiController,但是ASP.NET Web API框架本身只要 ...

  7. ASP.NET Web API 中的异常处理(转载)

    转载地址:ASP.NET Web API 中的异常处理

  8. 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化

    谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...

  9. Asp.Net Web API 2第十三课——ASP.NET Web API中的JSON和XML序列化

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

随机推荐

  1. Heapify

    Given an integer array, heapify it into a min-heap array. For a heap array A, A[0] is the root of he ...

  2. springcloud Zuul中路由配置细节

    上篇文章我们介绍了API网关的基本构建方式以及请求过滤,小伙伴们对Zuul的作用应该已经有了一个基本的认识,但是对于路由的配置我们只是做了一个简单的介绍,本文我们就来看看路由配置的其他一些细节. 首先 ...

  3. HTTP2.0新特性

    参考:http://www.tuicool.com/articles/mq2qm26

  4. java 异常与记录日志

    一. 你可能还想利用java.util.logging工具将输出记录到日志中 package exceptions; //: exceptions/LoggingExceptions.java // ...

  5. 当Python与数模相遇

    数模有一个题目要处理杭州自行车在每个站点可用数量和已经借出数量,这数据在www.hzbus.cn上可以获取,它是10分钟更新一次的.这些数据手动获取,需要不停的刷页面,从6:00am到9:00pm,显 ...

  6. 【LOJ】#2508. 「AHOI / HNOI2018」游戏

    题解 把没有门的点缩成一个点 如果\(i->i + 1\)的钥匙大于\(i\),那么\(i\)不可以到\(i + 1\),连一条\(i\)到\(i + 1\)的边 如果\(i->i + 1 ...

  7. rhev 虚拟化

    引用自:https://blog.csdn.net/Jmilk/article/details/50964121#rhev-hhypervisor-%E8%99%9A%E6%8B%9F%E6%9C%B ...

  8. JS代码浏览器兼容性 之 new Date()

    这里只测试3个浏览器的情况:IE, 火狐,谷歌. 一. 无参 //无参 var dateTime = new Date(); 所有浏览器都兼容,GOOD 二. 日期参数 //日期参数 格式1 var ...

  9. 004.Zabbix3.x-Server服务端安装

    一 环境基础 1.1 部署基础环境 部署Zabbix需要LAMP或LANP环境,数据库可以为MySQL或者MariaDB.硬件及存储条件按需配置. 1.2 常见依赖列表 Web前端需要支持的软件环境如 ...

  10. 转载-解决ORACLE 在控制台进行exp,导出时,空表不能导出

    一.问题原因: 11G中有个新特性,当表无数据时,不分配segment,以节省空间 1.insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segm ...