Adding a custom query backed Navigation Property to ODataConventionModelBuilder

You have to call “AddNavigationTarget” on the EntitySet.
Assume that your namespace is “MyNamespace”, then add the following code to your WebApiConfig.cs. In this way, retrieving the data with “Get: odata/Cars(1)/Parts” will work.

    var cars = (EdmEntitySet)edmModel.EntityContainers().Single().FindEntitySet("Cars");
    var parts = (EdmEntitySet)edmModel.EntityContainers().Single().FindEntitySet("Parts");
    var carType = (EdmEntityType)edmModel.FindDeclaredType("MyNamespace.Car");
    var partType = (EdmEntityType)edmModel.FindDeclaredType("MyNamespace.Part");

    var partsProperty = new EdmNavigationPropertyInfo();
    partsProperty.TargetMultiplicity = EdmMultiplicity.Many;
    partsProperty.Target = partType;
    partsProperty.ContainsTarget = false;
    partsProperty.OnDelete = EdmOnDeleteAction.None;
    partsProperty.Name = "Parts";

    cars.AddNavigationTarget(carType.AddUnidirectionalNavigation(partsProperty), parts);

Leave a Comment