产品上有一个功能可以将变体生成为列表。目前它返回:
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()"
}
}
}
}
我有点想知道应该调整什么来完成这项工作。