0
votes

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.

1
Did you try to use DataAnnotations property [Required]? - Roman Marusyk
@MegaTron I'm not sure what you mean. Do you mean a custom annotation to say that it is required? - TomDoesCode
If I proped understand you, I mean that if you want to do navigation property required then you can use DataAnnotations RequiredAttribute class - Roman Marusyk
@MegaTron I am building my metadata and objects at runtime from a database and not using concrete types so I don't think that I can use that. I have just updated my question to reflect that, sorry. - TomDoesCode

1 Answers

1
votes

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.