13.3 Multiple Navigation property binding path
Since Web API OData V6.0.0 beta, It supports to configure multiple navigation property binding paths.
Original navigation property binding
The following APIs are used to add navigation property binding in model builder.
1. HasManyBinding()
2. HasRequiredBinding()
3. HasOptionalBinding()
So, we can do as:
We can get the following result:
<EntityContainer Name="Container">
<EntitySet Name="Customers" EntityType="System.Web.OData.Builder.Cusomter">
<NavigationPropertyBinding Path="Orders" Target="Orders" />
<NavigationPropertyBinding Path="SingleOrder" Target="SingleOrders" />
</EntitySet>
<EntitySet Name="Orders" EntityType="System.Web.OData.Builder.Order" />
<EntitySet Name="SingleOrders" EntityType="System.Web.OData.Builder.Order" />
</EntityContainer>
Where, the binding path is straight-forward, just as the navigation property name. Before 6.0.0, it doesn’t support to:
- Add the complex property into binding path
- Add the type cast into binding path
Multiple navigation property binding path in model builder
In Web API OData V6.0.0 beta, we add a new generic type class to configure the multiple binding path:
In this class, it provides the following APIs to add binding path:
It also provides the following APIs to bind the navigation property with multiple binding path to a targe navigation source.
So, the normal navigation property binding configuration flow is:
- Call
Binding
fromNavigationSourceConfiguration{TEntityType}
to get an instance of BindingPathConfiguration{TEntityType} - Call
HasManyPath()/HasSinglePath
from BindingPathConfiguration{TEntityType}` to add a binding path - Repeat step-2 if necessary
- Call
HasManyBinding()/HasRequiredBinding()/HasOptionalBinding()
to add the target navigation source.
Here’s an example:
Example
Let’s have the following CLR classes as the model:
Entity types
Complex types
Add navigation property binding:
So, we can get the following target binding:
<Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityContainer Name="Container">
<EntitySet Name="Customers" EntityType="System.Web.OData.Builder.Cusomter">
<NavigationPropertyBinding Path="Pet/System.Web.OData.Builder.Human/HumanAddress/SubCity" Target="HumanCities" />
<NavigationPropertyBinding Path="Pet/System.Web.OData.Builder.Horse/HorseAddress/SubCity" Target="HorseCities" />
<NavigationPropertyBinding Path="Pet/System.Web.OData.Builder.Horse/HorseAddresses/SubCity" Target="HorseCities" />
<NavigationPropertyBinding Path="System.Web.OData.Builder.VipCustomer/VipLocations/System.Web.OData.Builder.UsAddress/SubCity" Target="B" />
<NavigationPropertyBinding Path="System.Web.OData.Builder.VipCustomer/VipLocations/City" Target="A" />
</EntitySet>
<EntitySet Name="A" EntityType="System.Web.OData.Builder.City" />
<EntitySet Name="B" EntityType="System.Web.OData.Builder.City" />
<EntitySet Name="HumanCities" EntityType="System.Web.OData.Builder.City" />
<EntitySet Name="HorseCities" EntityType="System.Web.OData.Builder.City" />
</EntityContainer>
</Schema>
Where: As example shown:
- It supports single property with multiple binding path.
- It supports collection property with mutliple binding path.
- It also supports mulitple binding path with inheritance type.
Besides, the HasManyPath()/HasSinglePath()
has onverload methods to configure the containment binding path.
Multiple navigation property binding path in convention model builder
In convention model builder, it will automatically traverl all property binding paths to add a binding for the navigation proeprty.
There’s an unit test that you can refer to.