0

产品上有一个功能可以将变体生成为列表。目前它返回:

The related entity set could not be found from the OData path

这是我的 WebApiConfig:

builder.EntityType<Product>().Function("GenerateVariants").Returns<List<ProductVariant>>();
////.Parameter<string>("save").OptionalParameter = true;

这是我在 ProductController 上的方法

    //http://localhost:26696/odata/Products(b2a35842-7b68-e511-beda-6c71d92133bc)/GenerateVariants
    [HttpGet]
    [ODataRoute("Products({key})/GenerateVariants")]
    public async Task<IHttpActionResult> GenerateVariants([FromODataUri] Guid key) // bool save = false
    {
        var product = await db.Products.Include("option1").Include("option2").Include("option3").Where(el => el.Id == key).FirstOrDefaultAsync();
        List<ProductVariant> productVariants = product.GenerateProductVariants();
        //if (save)
        //{
        //    db.ProductVariants.AddRange(productVariants);
        //    await db.SaveChangesAsync();
        //}
        return Ok(productVariants);
    }

我实际上希望我的 productController 生成 ProductVariants 列表。但我目前的错误是这样的:

{
  "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":"   bij System.Web.OData.Formatter.Serialization.ODataFeedSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)\r\n   bij System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders)\r\n   bij System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- Einde van stacktracering vanaf vorige locatie waar uitzondering is opgetreden ---\r\n   bij System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   bij System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   bij System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n   bij System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"
      }
    }
  }
}

我有点想知道应该调整什么来完成这项工作。

4

1 回答 1

0

出于某种原因(我以为我都试过了),productVariantsController 中的以下更改是正确的(最初它在 ProductsController 中)

我最后的改变是:

            builder.EntityType<ProductVariant>()
            .Function("GenerateFromProduct")
            .ReturnsCollectionFromEntitySet<ProductVariant>("ProductVariants");

包括迁移路由(在 webapiconfig 中)和 ProductVariantsController。

于 2015-10-15T22:50:25.293 回答