RESTier OData Function returning EntitySet. “The related entity set could not be found from the OData path”

旧巷老猫 提交于 2020-01-17 12:24:08

问题


Added a function on RESTier OData

var ambilLocationsByMarketId = new EdmAction(ns, "AmbilLocationsByMarketId", location.GetEdmTypeReference(true), false, null);
model.AddElement(ambilLocationsByMarketId);
var entityContainer = (EdmEntityContainer)model.EntityContainer;
entityContainer.AddActionImport("AmbilLocationsByMarketId", ambilLocationsByMarketId);

Implemented the custom function in my DomainController

[HttpGet]
[EnableQuery]
[ODataRoute("AmbilLocationsByMarketId")]
public IHttpActionResult AmbilLocationsByMarketId()
{
    var locations = DbContext.Locations.Where(l => l.Name.Contains("Hotel")).Select(l => l);
    return Ok(locations);
}   

But I keep getting this result in return

{
  "error": {
    "code": "",
    "message": "An error has occurred.",
    "innererror": {
      "message": "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.",
      "type": "System.InvalidOperationException",
      "stacktrace": "",
      "internalexception": {
        "message": "The related entity set could not be found from the OData path. The related entity set is required to serialize the payload.",
        "type": "System.Runtime.Serialization.SerializationException",
        "stacktrace": "   at System.Web.OData.Formatter.Serialization.ODataFeedSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)\\\r\\\n   at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders)\\\r\\\n   at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\\\r\\\n--- End of stack trace from previous location where exception was thrown ---\\\r\\\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\\r\\\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\\r\\\n   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\\\r\\\n   at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"
      }
    }
  }
}

How do I assign the right return EntitySet Collection on RESTier? I wanted to return the list of objects/ IQueryable


回答1:


There are two places you need to modify to make it work:

1) You need to add a collection return type when declaring the action.

2) You need to add an EdmEntitySetReferenceExpression when adding the ActionImport.

Please see https://github.com/OData/RESTier/blob/master/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Domain/TrippinDomain.cs#L177-L189 for an example (though it is a FunctionImport but should apply to an ActionImport too).



来源:https://stackoverflow.com/questions/31758820/restier-odata-function-returning-entityset-the-related-entity-set-could-not-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!