How to Deserialize Delta<T> from JSON using oDataMediaTypeFormatter with Entity Framework Code First

February 6, 2013

Today I was struggling trying to work out how I can make use of the oDataMediaTypeFormatter in ASP.Net Web API when you only have a code first model to work with. For the impatient, scroll to the bottom for the quick solution!

The Problem

I’d been reading various articles about oData, Particularly the following:

What I was actually trying to do was to implement partial updates to resources using the PATCH verb, and it all seemed really simple on paper. However when it came down to it, there were several problems:

You can’t use the [FromData] attribute with a Delta<T> parameter as this throws an exception

JsonMediaTypeFormatter doesn’t seem to be able to deserialise to Delta<T>, contrary to what the TechBrijj article suggests.

spc

Given the above, I tried to follow the instructions for ‘Doing more oData’ from Alex J’s article hoping to make use of the oDataMediaTypeFormatter. From the article:

// Create the OData formatter and give it the model
ODataMediaTypeFormatter odataFormatter = new ODataMediaTypeFormatter(model);

spc

There were more issues though:

Since the article was written, the constructor is for the media type formatter is no longer public so can’t be used.

At this point I did what any upstanding developer would do, I swore loudly and went to lunch.

The Solution

Isn’t it annoying when things turn out to be so simple? Poring over the oData package source I found out it’s actually very easy to instantiate the media type formatter. Additionally, it was also a one liner to interpret the code first model (see below)

For additional reasons not explained here, I chose to wrap everything up in another custom MediaTypeFormatter, but anywhere you have a reference to an HttpContent object would work fine too.