【问题标题】:RESTier OData Function returning EntitySet. "The related entity set could not be found from the OData path"RESTier OData 函数返回 EntitySet。 “无法从 OData 路径中找到相关实体集”
【发布时间】:2015-10-23 21:06:30
【问题描述】:

在 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);

在我的 DomainController 中实现了自定义功能

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

但我不断得到这个结果作为回报

{
  "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()"
      }
    }
  }
}

如何在 RESTier 上分配正确的返回 EntitySet 集合?我想返回对象列表/ IQueryable

【问题讨论】:

  • 使用函数的API不是比添加元素更容易吗?类似于: FunctionConfiguration personFn = builder.EntityType().Function("GetFamilyMembers").Returns(); personFn.Parameter("方向");
  • @MarvinSmit 我们知道这一点。我们正在尝试在 OData 之上使用 RESTier。

标签: asp.net-mvc odata restier


【解决方案1】:

您需要修改两个地方才能使其工作:

1) 声明动作时需要添加collection返回类型。

2) 添加 ActionImport 时需要添加 EdmEntitySetReferenceExpression。

请参阅https://github.com/OData/RESTier/blob/master/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Domain/TrippinDomain.cs#L177-L189 获取示例(虽然它是 FunctionImport 但也应适用于 ActionImport)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多