2
votes

I have an OData v3 Web API project. It uses an Entity Framework Code First model.

The main class is Coupon. It has a List. What this really is, is a 2-element collection of subtypes ItemRequirement and BasketRequirement. I want to be able to say:

../odata/Coupons(5)/ItemRequirement

I can NOT get this to work.

First, in the EF class, I have added ItemRequirement as a [NotMapped] property (as the class already has a collection of the base class as a navigation property, and adding the other two as properties would just generate extraneous table keys and mess up the database unnecessarily. The Table-Per-Hierarchy in Code First is working great as is).

The ODataConventionModelBuilder() is not picking up ItemRequirement as a navigation property

I attempted to add it:

// GET odata/Coupons(5)/ItemRequirement
public ItemRequirement GetItemRequirement( [FromODataUri] decimal key)
{
   return db.Coupons.Where(m => m.CouponId == key).SelectMany(m => m.RedemptionPurchaseRequirements).OfType<ItemRequirement>().FirstOrDefault();
}

The URI will NEVER get into this code. I have found by adding an IODataRoutingConvention implementor that the ODataPath is set to navigation/key/unresolved.

I looked at this solution and it didn't help me, either:

Adding a custom query backed Navigation Property to ODataConventionModelBuilder

I don't know if the problem is the inheritance, the fact that the property is not mapped in EF, or what.

I have also found that this fails with a 404:

oData/PurchaseRequirementsBases(5)/myNamespace.ItemRequirement

Just what witchcraft is necessary to abstract the collection away so the OData consumer can see ItemRequirement as a valid property of Coupon?

1

1 Answers

0
votes

Can you try adding the navigation property explicitly?

odataConventionModelBuilder().Entity<Coupon>().HasOptional(coupon => coupon.ItemRequirement)