2

I have a database model to store sports results. There is a Team table and a Fixture table:

enter image description here

At the moment, as you can see, I have the following navigation properties:

  • AwayFixtures / HomeFixtures
  • AwayHistoricMatches / HomeHistoricMatches
  • AwayLiveScores / HomeLiveScores

I would like, instead to combine the two collections for each and have the following:

  • Fixtures
  • HistoricMatches
  • LiveScores

I'd still need to keep the separation in the database because I still want to know if a team was playing at home or away. The final aim is to expose it in a Web API odata feed so my final urls will be:

  • /odata/Teams(45)/Fixtures
  • /odata/Teams(45)/HistoricMatches
  • /odata/Teams(45)/LiveScores

I've done a bit of googling for this problem and haven't turned anything up which kind of leads me to believe it's not possible or I'm searching for the wrong keywords "entity combining navigation properties". Is there a way to change the navigation properties to match the above?

Edit: Or, is there a way I can add a custom navigation property only to my Web API and then return the 2 collections combined while keeping the EDM the same?

1 Answer 1

2

Great question. It's important to realize that your OData Web API can have a different data model from the data model you use in your database. So what you could do for example is define an entity type called Fixture, add that to your OData model, and then have your implementation of Get() for the FixturesController look like this:

public IEnumerable<Fixture> Get(ODataQueryOptions queryOptions)
{
    List<Fixture> fixtures = new List<Fixture>();
    var homeFixtures = queryOptions.ApplyTo(_db.HomeFixtures) as IQueryable<HomeFixture>;
    var awayFixtures = queryOptions.ApplyTo(_db.AwayFixtures) as IQueryable<AwayFixture>;
    fixtures.AddRange(ConvertToFixtures(homeFixtures));
    fixtures.AddRange(ConvertToFixtures(awayFixtures));
    return fixtures;
}

And do something similar for the other properties.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.