$扩展不工作。未在结果中返回的关联/子记录。 [UPDATE]

时间:2018-05-10 03:53:43

标签: c# .net odata asp.net-core-webapi edmx

希望你们都做得很好。

好吧,我用c#创建了webApi。 下面是生成“../../$metadata”结果的代码(在图像的底部)

enter image description here

目前,我已对结果进行了硬编码,因此应始终发送客户及其相关产品,以下是代码:

        public EdmEntityObjectCollection Get()
    {
        IEdmEntityType customerType = (IEdmEntityType)ODataUntypedSample.Model.FindType("NS.Customer");
        IEdmCollectionType collectionType = Request.ODataProperties().Path.EdmType as IEdmCollectionType;
        EdmEntityObjectCollection collection = new EdmEntityObjectCollection(new EdmCollectionTypeReference(collectionType));

        EdmEntityObject customer1 = new EdmEntityObject(customerType);
        customer1.TrySetPropertyValue("CustomerId", CUSTOMER1id);
        customer1.TrySetPropertyValue("FullName", "Jony Mark One");
        collection.Add(customer1);

        EdmEntityObject customer2 = new EdmEntityObject(customerType);
        customer2.TrySetPropertyValue("CustomerId", CUSTOMER2id);
        customer2.TrySetPropertyValue("FullName", "Pole Sam two");
        customer2.TrySetPropertyValue("Products", GetChildProduct());
        collection.Add(customer2);

        return collection;
    }

    private static EdmEntityObjectCollection GetChildProduct()
    {
        IEdmEntityType productType = (IEdmEntityType)ODataUntypedSample.Model.FindType("NS.Product");

        IEdmCollectionTypeReference entityCollectionType
                        = new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(productType, isNullable: false)));
        EdmEntityObjectCollection ec = new EdmEntityObjectCollection(entityCollectionType);

        EdmEntityObject product1 = new EdmEntityObject(productType);
        product1.TrySetPropertyValue("ProductId", 99);
        product1.TrySetPropertyValue("CustomerIdRef", CUSTOMER2id);
        ec.Add(product1);

        EdmEntityObject product2 = new EdmEntityObject(productType);
        product2.TrySetPropertyValue("ProductId", 88);
        product2.TrySetPropertyValue("CustomerIdRef", CUSTOMER2id);
        ec.Add(product2);

        return ec;
    }

当我使用“$ expand”浏览网址时,它只返回客户数据,它不会返回子/关联产品数据。任何人都可以解释一下,为什么会这样呢?

查询:http://localhost:12345/odata/Customers?$ expand =产品
结果:

[{"CustomerId":11,"FullName":"Jony Mark One"}, 
 {"CustomerId":22,"FullName":"Pole Sam two"}]

0 个答案:

没有答案