Roy Tinker
Roy Tinker

Reputation: 10152

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.

my data model

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:

With the body as follows:

{
    __count: 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!

Upvotes: 0

Views: 1612

Answers (2)

Pratik
Pratik

Reputation: 607

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

Upvotes: 2

Roy Tinker
Roy Tinker

Reputation: 10152

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.

Upvotes: 0

Related Questions