Replies: 1 comment
-
I've done more digging and discovered I was missing navigation properties at the entity set level. Revised, working code is once again attached for reference. That said, expansion-related behavioral challenges remain. I'm currently operating under the assumption that use of the EnableQuery attribute provides path of least resistance to the expansion challenge, but its presence generates runtime exceptions. Consider the following controller method with the attribute applied: [EnableQuery(MaxExpansionDepth = 10)]
public async Task<EdmEntityObjectCollection> Get(string @namespace) Invocation results in the following exception: The query specified in the URI is not valid. Could not find a property named 'Items' on type 'Microsoft.AspNetCore.OData.Formatter.Value.IEdmEntityObject'. The exception only occurs when the public class EntityDataSource : IDataSource
{
private readonly IModelProvider _modelProvider;
private readonly string _namespace;
private IEdmModel _model;
public EntityDataSource(IModelProvider modelProvider, string @namespace)
{
_modelProvider = modelProvider;
_namespace = @namespace;
}
public async Task<IEdmModel> GetModel()
{
if (_model == null)
_model = await _modelProvider.GetModel(_namespace).ConfigureAwait(false);
return _model;
}
public async Task<IEnumerable<IEdmEntityObject>> Get(ODataQueryOptionParser queryOptionParser)
{
//Note: queryOptionParser is not used for this example.
var entityType = _model.FindType("ns.Item") as IEdmEntityType;
var item = new EdmEntityObject(entityType);
item.TrySetPropertyValue("Name", "Item 1");
item.TrySetPropertyValue("ID", 1);
AppendChildren(item, 2);
var items = new HashSet<IEdmEntityObject>();
items.Add(item);
return items;
}
private void AppendChildren(EdmEntityObject parent, int itemDepth)
{
var detail = new EdmEntityObject(_model.FindType("ns.Detail") as IEdmEntityType);
detail.TrySetPropertyValue("Description", $"Detail {itemDepth - 1}");
detail.TrySetPropertyValue("ID", itemDepth - 1);
parent.TrySetPropertyValue("Detail", detail);
if (itemDepth < 5)
{
var item = new EdmEntityObject(_model.FindType("ns.Item") as IEdmEntityType);
item.TrySetPropertyValue("Name", $"Item {itemDepth}");
item.TrySetPropertyValue("ID", itemDepth);
parent.TryGetPropertyValue("Items", out var value);
var collection = value as EdmEntityObjectCollection;
collection.Add(item);
AppendChildren(item, itemDepth + 1);
}
}
} I am going to create an issue from this discussion and would greatly appreciate feedback. In an effort to reach more subject matter experts, I also posted to Stack Overflow (https://stackoverflow.com/questions/74964391/odata-dynamic-model-self-referencing-navigation-property-max-level-expansion). |
Beta Was this translation helpful? Give feedback.
-
Hello! I'm hoping someone out there is able to assist with an issue I'm currently facing. I have a scenario in which my EDM model is dynamic and contains a self-referencing navigation property. Source code is attached and based upon a sample project in this repo (https://github.com/OData/AspNetCoreOData/tree/main/sample/ODataDynamicModel).
If my request explicitly expands to a desired depth, the response payload is correct, per the following:
http://localhost:4527/odata/ns/Items?$expand=Detail,Items($expand=Detail,Items($expand=Detail,Items($expand=Detail,Items($expand=Detail,Items))))
How might I go about expanding N levels since depth won't be known in a real world scenario? Numerous attempts haven't worked and I'm a bit puzzled, to say the least. Here's a recent attempt:
http://localhost:4527/odata/ns/Items?$expand=Detail,Items($levels=max;$expand=Detail,Items)
I greatly appreciate any insight and/or assistance!
ODataDynamicModel.zip
Beta Was this translation helpful? Give feedback.
All reactions