导航属性不反映

本文关键字:属性 导航 | 更新日期: 2023-09-27 18:03:20

导航属性,我没有反映在我的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>

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

导航属性不反映

似乎你正在写你的GET请求

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

你需要这样写你的查询:

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

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

OData协议参考:11.2.4.2系统查询选项$expand.