2014-07-15 36 views
0

我儘量深,獲得的OData,我已經創建了以下元數據服務:鏈接兩個實體的OData

<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0"> 
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="2.0"> 
<Schema xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="HelloOdata.library"> 
<EntityType Name="booksType"> 
<Key> 
<PropertyRef Name="title"/> 
</Key> 
<Property Name="title" Type="Edm.String" Nullable="false" MaxLength="100"/> 
<Property Name="ISBN" Type="Edm.String" MaxLength="100"/> 
<Property Name="editions" Type="Edm.Int32"/> 
<NavigationProperty Name="Author" Relationship="HelloOdata.library.WroteType" FromRole="booksPrincipal" ToRole="writersDependent"/> 
</EntityType> 
<EntityType Name="writersType"> 
<Key> 
<PropertyRef Name="name"/> 
<PropertyRef Name="birth"/> 
</Key> 
<Property Name="name" Type="Edm.String" Nullable="false" MaxLength="100"/> 
<Property Name="birth" Type="Edm.DateTime" Nullable="false"/> 
<NavigationProperty Name="Titles" Relationship="HelloOdata.library.BooksType" FromRole="writersPrincipal" ToRole="booksDependent"/> 
</EntityType> 
<Association Name="WroteType"> 
<End Type="HelloOdata.library.booksType" Role="booksPrincipal" Multiplicity="*"/> 
<End Type="HelloOdata.library.writersType" Role="writersDependent" Multiplicity="1"/> 
</Association> 
<Association Name="BooksType"> 
<End Type="HelloOdata.library.writersType" Role="writersPrincipal" Multiplicity="1"/> 
<End Type="HelloOdata.library.booksType" Role="booksDependent" Multiplicity="*"/> 
</Association> 
<EntityContainer Name="library" m:IsDefaultEntityContainer="true"> 
<EntitySet Name="books" EntityType="HelloOdata.library.booksType"/> 
<EntitySet Name="writers" EntityType="HelloOdata.library.writersType"/> 
<AssociationSet Name="Wrote" Association="HelloOdata.library.WroteType"> 
<End Role="booksPrincipal" EntitySet="books"/> 
<End Role="writersDependent" EntitySet="writers"/> 
</AssociationSet> 
<AssociationSet Name="Books" Association="HelloOdata.library.BooksType"> 
<End Role="writersPrincipal" EntitySet="writers"/> 
<End Role="booksDependent" EntitySet="books"/> 
</AssociationSet> 
</EntityContainer> 
</Schema> 
</edmx:DataServices> 
</edmx:Edmx> 

你有任何想法如何創建一個寫一本書之間的聯繫?我試圖閱讀OData文檔中的示例,但是我沒有取得成功和嘗試。我正在使用OData數據協議v2。

謝謝。 巴勃羅

嗯,我創造了這個POST請求:

POST /HelloOdata/library.xsodata/books('HAPI%20Hana%20Integration%20Test%20Volume%201')/$links/Author HTTP/1.1 
Host: xxxxx 
DataServiceVersion: 1.0 
MaxDataServiceVersion: 2.0 
Content-Type: application/xml 

<uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices"> 

http://host-service/HelloOdata/library.xsodata/writers(name='Robert%20Kingst',birth=datetime'2008-04-08T00:00:00.0000000') 

</uri> 

和我有以下回應:

<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> 
<code /> 
<message xml:lang="en-US">The URI is not valid for POST operation. The URI must point to an entity set for POST operations.</message> 
</error> 
+0

你是什麼意思的鏈接?我認爲你已經擁有導航屬性。外鍵是你想要的嗎? –

+0

嗯,我可以通過POST請求插入書籍和作家,但我沒有找到一種方式來指定某個作者是某本書的作者。這就是我想要做的。 –

回答

0

假設你想作者「查爾斯·狄更斯」鏈接書「Oliver Twist」,然後您發送POST請求到您的服務:

POST http://host/service/booksType('Oliver Twist')/$links/Author 

<uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices"> 

http://host/service/writersType(name='Charles Dickens',birth='1812-02-07T00:00:00') 

</uri> 
+0

我在上面的問題中發佈了POST請求的錯誤消息。 –

0

由於作者不是集合,請嘗試將POST方法更改爲PUT。