13.2 Complex Type with Navigation Property
Since Web API OData V6.0.0 beta, It supports to configure navigation property on complex type.
Let’s have an example to illustrate how to configure navigation property on complex type:
CLR Model
We use the following CLR classes as the CLR model:
Where:
- Address is a complex type.
- City is an entity type.
Add navigation properties
The following APIs are used to add navigation properties for complex type:
So, we can do as:
We can get the following result:
<ComplexType Name="Address">
<NavigationProperty Name="CityInfo" Type="ModelLibrary.City" Nullable="false" />"
<NavigationProperty Name="Cities" Type="Collection(ModelLibrary.City)" />"
</ComplexType>
<EntityType Name="City">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
</EntityType>
Add navigation properties in convention model builder
Convention model builder will automatically map the class type properties in complex type as navigation properties if the declaring type of such navigation property has key defined.
So, as the above example, we can use the following codes to define a convention model:
As result, We can get the following result:
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="ModelLibrary" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<ComplexType Name="Address">
<NavigationProperty Name="CityInfo" Type="ModelLibrary.City" />
<NavigationProperty Name="Cities" Type="Collection(ModelLibrary.City)" />
</ComplexType>
<EntityType Name="City">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
</EntityType>
</Schema>
<Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityContainer Name="Container" />
</Schema>
</edmx:DataServices>
</edmx:Edmx>