Matthew Merryfull
Matthew Merryfull

Reputation: 1496

OData v4 Related Entities

I'm currently working through Mike Wasson's tutorial on getting OData v4 up and running (referenced here). The problem is I'm unable to get related entities up and running for some reason. It always returns a HTTP 404 with the route being unable to be found.

Now I've looked here and here but both of these answers do not compile as the namespacing is all wrong and the methods have been somewhat updated since the OPs asked the questions.

The tutorial makes it seem like in order to enable the following related entities request:

http://localhost/odata/Suppliers(1)/Products

all I would need to do is implement the following method on the controller SuppliersController

[EnableQuery]
public IQueryable<Product> GetProducts([FromODataUri] int key)
{
    return _repository.List().Where(s => s.SupplierId == key).SelectMany(s => s.Products);
}

But no matter what I try and pull off, this simply doesn't work. Is their anything in particular I've missed that should be put in place for navigational properties or is there any additional configuration that needs to be put in place?

Any help greatly appreciated.

Upvotes: 1

Views: 757

Answers (1)

Matthew Merryfull
Matthew Merryfull

Reputation: 1496

So after a gruelling night of searching for answers, I went over the routing table and found that the following method on the builder:

var builder = new ODataConventionModelBuilder();

builder.EnableLowerCamelCase();

Is to blame...sort of. You see, the property names on my data models all start with a capital letter as is the standard for .net, however, JS its camel cased so http://localhost/odata/Tables(1)/Messages is actually http://localhost/odata/Tables(1)/messages note the lowercase "m" on the method

This little gotcha extends to Related Entities which look to be treated the same way.

So a word to the wise when creating your edm Model, .net respects (as it should) the Entity Set name you provide to the builder. If you want to make sure that everything is camel cased, then you best make sure you declare your EntitySets with a leading lowercase character.

Upvotes: 1

Related Questions