I'm not sure if this is an issue with the most recent Microsoft.OData.Core, or if it is a change in the newest OData v4 Client code generator. However, the client code generator is now creating nullable foreign keys on int fields that are not set to nullable int.
As discussed here: https://visualstudiogallery.msdn.microsoft.com/9b786c0e-79d1-4a50-89a5-125e57475937
The solution to the issue is to set your [ForeignKey("ColumnName")]
to [Required, ForeignKey("ColumnName")]
. The $metadata will then report the corresponding foreign key int field as nullable=false, and the Client Code Generator will correctly produce a non nullable int.
This was not necessary prior to my most recent upgrade of Microsoft.OData.Core
and the OData v4 Client Code Generator, however since I upgraded them both at the same time I can't identify the culprit.