Search code examples
c#asp.net-web-apiodata

Navigation Property does not reflect


The navigation property that i have doesnt reflect on my odata results but I am assigning values on that property. Here's the code.

for (var x = 1; x <= 5; x++)
            {
                var mainEntity = new EdmEntityObject(entityType);
                mainEntity.TrySetPropertyValue("Id", "AllKey" + x.ToString());
                mainEntity.TrySetPropertyValue("UserName", "AccountId" + x.ToString());
                mainEntity.TrySetPropertyValue("EmailAddress", "[email protected]" + x.ToString());
                mainEntity.TrySetPropertyValue("FirstName", "FirstName" + x.ToString());
                mainEntity.TrySetPropertyValue("LastName", "LastName" + x.ToString());
                mainEntity.TrySetPropertyValue("Custom", "Custom" + x.ToString());
                mainEntity.TrySetPropertyValue("[BasicProperty]Id", "[BasicProperty]Id" + x.ToString());

                IEdmEntityTypeReference categoryType = entityType.FindNavigationProperty("UserLogin").Type.AsEntity();

                var mainEntity2 = new EdmEntityObject(categoryType);
                mainEntity2.TrySetPropertyValue("Id", "AllKey" + x.ToString());
                mainEntity2.TrySetPropertyValue("UserName", "AccountId" + x.ToString());
                mainEntity2.TrySetPropertyValue("EmailAddress", "[email protected]" + x.ToString());
                mainEntity2.TrySetPropertyValue("FirstName", "FirstName" + x.ToString());
                mainEntity2.TrySetPropertyValue("LastName", "LastName" + x.ToString());

                mainEntity.TrySetPropertyValue("UserLogin", mainEntity2);                    

                collectionProduct.Add(mainEntity);
            }

Here's the result.

{

"@odata.context": "http://localhost/WebApi/enwisen/User_Entity/$metadata#User_Entity",
"value": [
    {
        "UserName": "AccountId1",
        "EmailAddress": "[email protected]",
        "Id": "AllKey1",
        "FirstName": "FirstName1",
        "LastName": "LastName1",
        "Custom": "Custom1"
    },
    {
        "UserName": "AccountId2",
        "EmailAddress": "[email protected]",
        "Id": "AllKey2",
        "FirstName": "FirstName2",
        "LastName": "LastName2",
        "Custom": "Custom2"
    },
    {
        "UserName": "AccountId3",
        "EmailAddress": "[email protected]",
        "Id": "AllKey3",
        "FirstName": "FirstName3",
        "LastName": "LastName3",
        "Custom": "Custom3"
    },
    {
        "UserName": "AccountId4",
        "EmailAddress": "[email protected]",
        "Id": "AllKey4",
        "FirstName": "FirstName4",
        "LastName": "LastName4",
        "Custom": "Custom4"
    },
    {
        "UserName": "AccountId5",
        "EmailAddress": "[email protected]",
        "Id": "AllKey5",
        "FirstName": "FirstName5",
        "LastName": "LastName5",
        "Custom": "Custom5"
    }
]

}

Here's the metadata

<EntityType Name="User">
<Key>
   <PropertyRef Name="Id"/>
</Key>
<Property Name="UserName" Type="Edm.String"/>
<Property Name="EmailAddress" Type="Edm.String"/>
<Property Name="Id" Type="Edm.String"/>
<Property Name="FirstName" Type="Edm.String"/>
<Property Name="LastName" Type="Edm.String"/>
<Property Name="Custom" Type="Edm.String"/>
<NavigationProperty Name="GroupLookup" Type="Collection(Enwisen.GroupLookup)"/>
<NavigationProperty Name="UserLogin" Type="Enwisen.UserLogin" Nullable="false/>
</EntityType>

Notice that I have a navigation property but it doesnt reflect on the result.


Solution

  • It seems like that you are writing your GET request as

    http://localhost/WebApi/enwisen/User_Entity/User_Entity
    

    You need to write your query as follows:

    http://localhost/WebApi/enwisen/User_Entity/User_Entity?$expand=UserLogin
    

    Thus the navigation property will be shown in the response payload.

    OData protocol reference: 11.2.4.2 System Query Option $expand.