2

I have an entity with a composite primary key, and I can retrieve a single instance of it using:

GET https://example.com/service/Contacts(Foo=3,Bar=18) 

How can I update an instance of it? I tried a PATCH using the same address:

PATCH https://example.com/service/Contacts(Foo=3,Bar=18) 

But I get the next error:

{
    "error" : {
        "code" : "",
        "message" : "The request is invalid.",
        "innererror" : {
            "message" : "key : Expected literal type token but found token 'Foo'.\r\n",
            "type" : "",
            "stacktrace" : ""
        }
    }
}

What does this error mean?

I also tried without the property names, but I get another error:

PATCH https://example.com/service/Contacts(3,18) 

Cannot create an abstract class. Description: An unhandled exception occurred during the execution of the current web request. Please review the stacktrace for more information about the error and where it originated in the code.

Any idea how can I do a PATCH in this case?

Thank you.

T.S.
  • 18,195
  • 11
  • 58
  • 78
kiewic
  • 15,852
  • 13
  • 78
  • 101

1 Answers1

0

May be I am too late to the party but here it goes for PATCH

Note, that this is OData v6 (System.Web.OData) and Entity Framework with manual model setup. I think, previous version of OData didn't support this out of box.

In the controller, action Patch must have arguments that composed from 'key'+{ModelPropertyName}. Considering your model having properties Foo and Bar, the controller action should be as following.

public async Task<IHttpActionResult> Patch([FromODataUri] int keyFoo, [FromODataUri] int keyBar, Delta<FooBarModel> modelDelta)

Note that casing on the arguments don't matter. You can have them like KeyFoO if you want. But it is really matter on the URL

PATCH https://example.com/service/Contacts(Foo=3,Bar=18)

Here casing is matter. This wouldn't work

PATCH https://example.com/service/Contacts(foo=3,bar=18)

Order don't matter, this will work

PATCH https://example.com/service/Contacts(Bar=18, Foo=3)

This doesn't work

PATCH https://example.com/service/Contacts(3,18)

T.S.
  • 18,195
  • 11
  • 58
  • 78