使用ODataLib条目编写器时如何创建ODataNavigationLink?

使用ODataLib条目编写器时如何创建ODataNavigationLink?,odata,odatalib,Odata,Odatalib,我试图找到一个例子,说明如何在ODataNavigationLink非空的情况下正确实例化它。我发现的唯一代码示例创建了一个非扩展链接,但没有将其绑定到任何数据: //create a non-expanded link for the orders navigation property writer.WriteStart(new ODataNavigationLink() { IsCollection = true, Name = "Orders",

我试图找到一个例子,说明如何在ODataNavigationLink非空的情况下正确实例化它。我发现的唯一代码示例创建了一个非扩展链接,但没有将其绑定到任何数据:

//create a non-expanded link for the orders navigation property
  writer.WriteStart(new ODataNavigationLink()
  {
      IsCollection = true,
      Name = "Orders",
      Url = new Uri("http://microsoft.com/Customer(" + 
                 dataSource.Customers.First().CustomerID + ")/Orders")
  });
  writer.WriteEnd(); //ends the orders link

因此,这里我们指定指向“订单”的链接。但是如何提供链接的实际值(在本例中,链接是一个集合,但也可以是单个条目)。当我手动写入有效负载时,我提供了带有链接条目ID的“href”属性。我不知道如何使用ODataLib实现这一点。

在“href”属性中显示的值仅显示ODataNavigationLink的Url属性,因此您可以尝试以下代码手动设置它:

//create a non-expanded link for the orders navigation property
writer.WriteStart(new ODataNavigationLink() { 
    IsCollection = true,
    Name = "Orders",
    Url = new Uri("http://microsoft.com/Orders(3)") }); 
writer.WriteEnd(); //ends the orders link
通常,导航链接应该是源实体url,后跟导航属性,请参见,而id应该指向真正的条目id

更新:

根据最新的反馈,您正试图编写本手册第14.1节所述的“链接集合”。因此,您可以尝试ODataEntityReferenceLink类:

var referenceLink1 = new ODataEntityReferenceLink { Url = new Uri("http://host/Orders(1)") };
var referenceLink2 = new ODataEntityReferenceLink { Url = new Uri("http://host/Orders(2)") };
var referenceLink3 = new ODataEntityReferenceLink { Url = new Uri("http://host/Orders(3)") };
var referenceLinks = new ODataEntityReferenceLinks
{
    Links = new[] { referenceLink1, referenceLink2, referenceLink3 }
};
writer.WriteEntityReferenceLinks(referenceLinks);
有效载荷是这样的:

<links xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
  <uri>http://host/Orders(1)</uri>
  <uri>http://host/Orders(2)</uri>
  <uri>http://host/Orders(3)</uri>
</links>

http://host/Orders(1)
http://host/Orders(2)
http://host/Orders(3)

Hmm,但“订单”是一个集合,如何指定多个项目?通过设置多个链接?我想我已经试过这样的东西了,但没有用。我会再检查一遍。你能在这里显示你期望的实际有效载荷吗?如果是这样,您可以尝试ODataEntityReferenceLink类,然后使用writer.WriteEntityReferenceLink。是的,我认为ODataEntityReferenceLink是一种方法。我尝试了一个小示例,并将“href”属性设置为引用的实体键。我会做更多的测试。如果你编辑你的答案并添加对ODataEntityReferenceLink的引用,我会接受它作为答案,因为这正是我需要的。