OData V4客户端添加子实体 [英] OData V4 client adding child entity

查看:104
本文介绍了OData V4客户端添加子实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父级(订单)和一个子级(OrderDetail),其中数据库中已经存在Order,数据库中也存在OrderDetail. 我真正想做的就是添加另一个与Order绑定的OrderDetail记录.

I have a parent (Order) and child (OrderDetail) where Order already exists in the database and OrderDetail also exists in the database.
All I really want to do is add another OrderDetail record bound to the Order.

我走了好几条路,我什至不确定什么是正确的路.
让我们假设它们之间的导航已经起作用.
我可以罚款$ expand = OrderDetails,也可以罚款Orders(1)/OrderDetails,并从OrderDetails进行相反的操作.

I have been down several paths and I'm not even sure what is the correct path.
Let's make some assumptions that navigations between these are already working.
I can $expand=OrderDetails fine and I can also Orders(1)/OrderDetails fine and do the reverse from OrderDetails.

基于此 更新数据服务 ,我需要做的就是调用AddRelatedObject,然后将对象添加到OrderDetails集合中.

Based on this Updating the Data Service, all I need to do is call AddRelatedObject and then Add the object to the OrderDetails collection.

// Add the new item with a link to the related Order
context.AddRelatedObject(order, "OrderDetails", newOrderDetail);
// Add the new order detail to the collection
order.Order_Details.Add(newOrderDetail);
newOrderDetail.Order = order;

看起来很简单.
但是,当我执行context.SaveChanges(SaveChangesOptions.ReplaceOnUpdate)时,它将引发错误.

Seems simple enough.
Yet when I execute context.SaveChanges(SaveChangesOptions.ReplaceOnUpdate) it will throw an error.

{错误":{代码":",消息":未找到与请求URI'

{"error":{"code":"","message":"No HTTP resource was found that matches the request URI 'http://localhost/Test/odata/Orders(1)/OrderDetails'.","innererror":{"message":"No routing convention was found to select an action for the OData path with template '~/entityset/key/navigation'.","type":"","stacktrace":""}}}

但是,如果我导航到列出的URL,它将显示数据.
提琴手的时间.
在Fiddler中,我可以看到这是URL的POST,而不是GET.
应该是POST而不是列出的URL.
POST应该已经到/odata/OrderDetails

But if I navigate to the URL listed, it shows data.
Time for Fiddler.
In Fiddler, I can see this is a POST to the URL and not a GET.
Which it should be a POST but not to the URL listed.
The POST should have been to /odata/OrderDetails

第二回合

// Add the new item with a link to the related Order
context.AttachTo("OrderDetails", newOrderDetail);
// Add a link between Order and the new OrderDetail
context.AddLink(order, "OrderDetails", newOrderDetail);
// Add the new order detail to the collection
order.Order_Details.Add(newOrderDetail);
newOrderDetail.Order = order;

仍然是POST并带有错误,但URL略有不同,并且发布的json仅具有"/odata/OrderDetail(0)",现在还具有"$ ref".

Still a POST with an error but the URL is slightly different and the json posted only has "/odata/OrderDetail(0)" it also now has "$ref".

{错误":{代码":",消息":未找到与请求URI'

{"error":{"code":"","message":"No HTTP resource was found that matches the request URI 'http://localhost/Test/odata/Orders(1)/OrderDetails/$ref'.","innererror":{"message":"No routing convention was found to select an action for the OData path with template '~/entityset/key/navigation/$ref'.","type":"","stacktrace":""}}}

通过快速的网络搜索,我找到了这篇文章 我确实在Orders控制器中创建了一个"CreateRef",并确保它足够被调用,但本文假设OrderDetail存在于数据库中.
它没有发布json OrderDetail对象.

Well a quick web search led me to this article Entity Relations in OData v4 Using ASP.NET Web API 2.2
This article says I need to add a "CreateRef" in the Orders controller.
I did create a "CreateRef" in the Orders controller and sure enough it gets called BUT the article assumes the OrderDetail exists in the database.
It is not posting the json OrderDetail object.

第三回合

// Add the new item with a link to the related Order
context.AttachTo("OrderDetails", newOrderDetail);
// Attach a link between Order and the new OrderDetail
context.AttachLink(order, "OrderDetails", newOrderDetail);
// Add the new order detail to the collection
order.Order_Details.Add(newOrderDetail);
newOrderDetail.Order = order;

这看起来好多了.
没有错误,但没有完全起作用.
它向/odata/OrderDetails(0)发送了一个PUT,并且确实向json发送了OrderDetail对象,但这应该是 POST而不是PUT .

Well this seems much better.
No error but it did not fully work.
It sent a PUT to the /odata/OrderDetails(0) and it did send the json OrderDetail object BUT this should have been a POST not a PUT.

我觉得我已经很近了,但我似乎还不知道如何使它正常工作.

I feel I am so close yet I can't seem to figure out how to get it to work properly.

有什么想法吗?

推荐答案

经过反复试验,我发现了一些可行的方法.

After trial and error I found something that worked.

// create a new order detail
OrderDetail newOrderDetail = new OrderDetail();
// set the orderID on the new order detail
newOrderDetail.OrderID = order.ID;
// add the order back as a link on the order detail
newOrderDetail.Order = order;
// add the order detail to the order detail collection on the order
order.OrderDetails.Add(newOrderDetail);
// add the order detail to the context
context.AddToOrderDetail(newOrderDetail);
// now update context for the order
context.UpdateObject(order);
// now save
context.SaveChanges();

这篇关于OData V4客户端添加子实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆