DataServiceContext:更新导航集合属性 [英] DataServiceContext : Update Navigation Collection Property
问题描述
我在的OData v4的客户端消耗的OData WebApi.2.1服务。
I'm consuming a Odata WebApi.2.1 Service in a Odata v4 client.
当我尝试更新的实体,我收到以下错误:
When I attempt to update the entity, and I'm getting following error:
的UpdateRelatedObject方法,只有当在SourceProperty不是集作品的
下面我有code在我的申请。
I have below code in my application.
public class Customer
{
int CustomerId;
string CustomerName;
ICollection<Order> Orders;
}
public void Save()
{
foreach (var item in Customer.Orders)
{
Context.UpdateRelatedObject(Customer, "Orders", item);
}
Context.UpdateObject(Customer);
Context.SaveChanges();
}
在这里,订单是一流的客户的导航属性。我怎样才能解决这个问题?
Here, "Orders" is a navigation property of class Customer. How can i solve this?
推荐答案
拉胡尔
基本上有两个规则:
- 这是无效的 发表的以任何非集合值导航属性。
- 这是无效的 把/补丁的以任何集合值导航属性。
公共API UpdateRelatedObject
中的OData客户端用于更新非集合属性。在它的开源,也有codeS为:
The public api UpdateRelatedObject
in OData client is designed to update the non-collection property. In its open source, there are codes as:
public void UpdateRelatedObject(object source, string sourceProperty, object target)
{
...
ClientPropertyAnnotation property = parentType.GetProperty(sourceProperty, false);
if (property.IsKnownType || property.IsEntityCollection)
{
throw Error.InvalidOperation(Strings.Context_UpdateRelatedObjectNonCollectionOnly);
}
...
}
所以,你的错误信息被抛出在上面罚球
语句。
通常情况下,你可以做如下,使其工作:
Normally, you can do as follows to make it work:
- 查询相关订单,例如:GET〜/客户(2)/令(1)
- 修改返回的订单对象
- 更新修改后才能拨打:UpdateObject
希望它可以帮助你。谢谢你。
Hope it can help you. Thanks.
这篇关于DataServiceContext:更新导航集合属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!