How to link two existing items (many-to-many) via the OData HTTP protocol?
I'm working with an EF data model which is exposed through an OData service endpoint. It has Person and Group entities which are connected through a many-to-many relationship.
I would like to add a link from an existing Person with an existing Group through OData, but I have been unable to figure out how. The OData protocol as specified here (link to odata.org) seems to specify that I need to do the following HTTP request to link Person #1 to Group #1:
- Method: POST
- URI: "http://localhost:49432/MyService.svc/Persons(1)/$links/Groups"
With the body as follows:
{
__c开发者_如何转开发ount: 1,
results: [{
uri: "http://localhost:49432/MyService.svc/Groups(1)"
}]
}
However, when I do that, I get the following error:
400 Bad Request
"Missing URI element. For link operations, URI element must be specified."
I have no idea what URI element is missing. I have provided that in both the request URI and in the payload...
I have also tried several other approaches, but no success so far. Any suggestions?
Thanks!
If you want to do more than one link, here's how you do it:
- Method: MERGE
- URI: "http://localhost:49432/MyService.svc/Persons(1)"
Payload should look something like this:
{
Groups: [ { __metadata: { uri:"http://localhost:49432/MyService.svc/Groups(1)" },
{ __metadata: { uri:"http://localhost:49432/MyService.svc/Groups(2)" }
]
}
Hope this helps.
Thanks Pratik
I had it right, except for the body of the request. It should have been much simpler:
{ uri: "http://localhost:49432/MyService.svc/Groups(1)" }
Sweet!
[EDIT]
Just a note for future reference: it looks like only one link can be created at a time using this method.
精彩评论