Search code examples
c#asp.net-web-apiodataodata-v4

Required navigation property with OData create


With OData, we can set the IsNullable value for a structural property to false to say that a property is required.

Is it possible to do something similar for a navigation property?

I have found the TargetMultiplicity property on EdmNavigationPropertyInfo which can be set to a value such as EdmMultiplicity.One. I assumed this would make it required (and may do under the hood) but there doesn't appear to be anything in the metadata to show that it is not nullable so the consumer won't know that it is required.

I am building my metadata and objects at runtime from a database and not using concrete types.


Solution

  • For you question:

    Is it possible to do something similar for a navigation property?

    The answer is absolutely Yes.

    Let me illustrate form following aspects:

    1. From OData v4 CSDL spec, it says that:

    The edm:NavigationProperty element MAY contain the Nullable attribute whose Boolean value specifies whether a navigation target is required for the navigation property. ....

    1. Multiplicity is the concept in OData V3 spec. you can find the definition 10.2.3 The edm:Multiplicity Attribute from here. Because the implementation of OData V4 is migrated from OData V3 library. So, the same definition is reused(not changed) in OData V4 library. However, it should be removed from OData V4 library.

    2. From the OData v4 lib implementation, you can find the following mapping:

      a) EdmMultiplicity.ZeroOrOne (IsNullable = true)

      b) EdmMultiplicity.One (IsNullable = false)

      c) EdmMultiplicity.Many ( Means the collection, while collection is always non-nullable)

    So, I say yes.


    Let me show the example: If you try the OData sample service: http://services.odata.org/v4/TrippinService/$metadata, you can find Nullable attribute for the navigation property.

    --

    <Property Name="FlightNumber" Nullable="false" Type="Edm.String"/>
    
    <NavigationProperty Name="From" Nullable="false" Type="Microsoft.OData.SampleService.Models.TripPin.Airport"/>
    
    <NavigationProperty Name="To" Nullable="false" Type="Microsoft.OData.SampleService.Models.TripPin.Airport"/>
    
    <NavigationProperty Name="Airline" Nullable="false" Type="Microsoft.OData.SampleService.Models.TripPin.Airline"/>
    
    </EntityType>
    

    Hope it can help you. Thanks.