导航属性没有反映出来

时间:2014-05-15 03:39:53

标签: c# asp.net-web-api odata

我所拥有的导航属性并不反映我的odata结果,但我在该属性上分配值。这是代码。

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", "EmailAddress@EmailAddress.com" + 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", "EmailAddress@EmailAddress.com" + x.ToString());
                mainEntity2.TrySetPropertyValue("FirstName", "FirstName" + x.ToString());
                mainEntity2.TrySetPropertyValue("LastName", "LastName" + x.ToString());

                mainEntity.TrySetPropertyValue("UserLogin", mainEntity2);                    

                collectionProduct.Add(mainEntity);
            }

这是结果。

{

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

}

这是元数据

<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>

请注意,我有一个导航属性,但它没有反映结果。

1 个答案:

答案 0 :(得分:1)

您似乎正在编写GET请求

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

您需要按如下方式编写查询:

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

因此导航属性将显示在响应有效负载中。

OData协议参考:11.2.4.2 System Query Option $expand