/odata/Products?$expand=Category not working
HI!
In "ODataUntypedSample" project, when I tried to get the Product's as well as its Category by using "/odata/Products?$expand=Category" - it didn't return the Category records. Can anyone explain why... Is i am missing something?
[tried both "ODataUntypedSample" as well as "ODataUntypedSample6.x"]
Any Suggestion ?
Hi, I am facing similar issue regarding $expand. While querying "~/odata/Products?$expand=Category" -no records from category are returned. Can anyone please help me?
@sinhatripti2509 Would you please check whether you enable the Expand() or not? Besides, untyped scenario doesn't support $expand.
I have tried the solution which you provided but still the navigation properties are not getting displayed from the collection. Problem Statement: I am creating models dynamically by taking schema information from sparql remote endpoint (graph db). To create edm models dynamically, I have taken reference from here https://github.com/OData/ODataSamples/tree/master/WebApiCore/DynamicEdmModelCreation. Get all , $filter , $count , $top & $select are working fine , but $expand is not giving expected output. Below i have given an example, GetModel() , which I have taken reference to create navigation properties. Can there be issue due to settings in navigation properties??
GetModel(): private void AddProductModel(EdmModel model, EdmEntityContainer container) { IDictionary<string, EdmEntityType> map = new Dictionary<string, EdmEntityType>();
EdmEntityType product = new EdmEntityType("ns", "Product");
map.Add("Product", product);
product.AddStructuralProperty("Name",
EdmPrimitiveTypeKind.String); EdmStructuralProperty key = product.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32); product.AddKeys(key); model.AddElement(product); EdmEntitySet products = container.AddEntitySet("Products", product);
EdmEntityType detailInfo = new EdmEntityType("ns",
"DetailInfo"); detailInfo.AddKeys(detailInfo.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); detailInfo.AddStructuralProperty("Title", EdmPrimitiveTypeKind.String); model.AddElement(detailInfo); EdmEntitySet detailInfos = container.AddEntitySet("DetailInfos", product);
EdmNavigationProperty detailInfoNavProp =
product.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "DetailInfo", TargetMultiplicity = EdmMultiplicity.One, Target = detailInfo,
});
products.AddNavigationTarget(detailInfoNavProp, detailInfos);
}
Controller: public EdmEntityObjectCollection Get() { // Get entity set's EDM type: A collection type. ODataPath odataPath = Request.ODataFeature().Path;
var model = DataSourceProvider.GetEdmModel();
IEdmType actualEntityType = odataPath.EdmType;
ODataQueryContext queryContext = new ODataQueryContext(model,
actualEntityType, odataPath);
ODataUriParser uriParser = new ODataUriParser(model, new
System.Uri(odataPath.ToString() + Request.QueryString.ToString(), uriKind: System.UriKind.Relative)); var actualEntityName = (odataPath.ToString()).Replace("Collection", string.Empty); IEdmCollectionType collectionType = (IEdmCollectionType)odataPath.EdmType;
IEdmEntityTypeReference entityType =
collectionType.ElementType.AsEntity();
// Create an untyped collection with the EDM collection type.
EdmEntityObjectCollection collection = new
EdmEntityObjectCollection(new EdmCollectionTypeReference(collectionType)); ODataQueryParser queryParser = new ODataQueryParser(); queryParser.ODataQueryValidation(uriParser, actualEntityName, entityType, collection, c); return collection; }
GetData(): public void Get(IEdmEntityTypeReference entityType, EdmEntityObjectCollection collection) { EdmEntityObject entity = new EdmEntityObject(entityType); entity.TrySetPropertyValue("Name", "abc"); entity.TrySetPropertyValue("ID", 1); entity.TrySetPropertyValue("DetailInfo", CreateDetailInfo(88, "abc_detailinfo", entity.ActualEdmType));
collection.Add(entity);
entity = new EdmEntityObject(entityType);
entity.TrySetPropertyValue("Name", "def");
entity.TrySetPropertyValue("ID", 2);
entity.TrySetPropertyValue("DetailInfo", CreateDetailInfo(99,
"def_detailinfo", entity.ActualEdmType));
collection.Add(entity);
}
in the above program, collection returned by controller holds the primitive values (Name,Id) as well as navigation property values (DetailInfo) but some how it is not displaying the detailInfo values after executing the program.
Url Request: ~/odata/Products?$expand=DetailInfo
Present Output: {"@odata.context":"http://localhost: (port)/odata/$metadata#Products","value":[{"Name":"abc","ID":1},{"Name":"def","ID":2}]}
Expected Output: {"@odata.context":"http://localhost:(port)/odata/$metadata#Products","value":[{"Name":"abc","ID":1, "DetailInfo":{"ID":88,"Title":"abc_detailinfo"}},{"Name":"def","ID":2, "DetailInfo":{"ID":99,"Title":"def_detailinfo"}}]}
On Fri, Aug 23, 2019 at 10:38 PM Sam Xu [email protected] wrote:
@sinhatripti2509 https://github.com/sinhatripti2509 Would you please check whether you enable the Expand() or not?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/OData/ODataSamples/issues/70?email_source=notifications&email_token=AMEWJA6W3GOWQHQCYOX4M7DQGAKRLA5CNFSM4E7N62RKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5AZJ2I#issuecomment-524391657, or mute the thread https://github.com/notifications/unsubscribe-auth/AMEWJA62UQAFJVFENNM3IMTQGAKRLANCNFSM4E7N62RA .