asp.net web api - UpdateRelatedObject method only works when the sourceProperty is not collection -
i getting following error when try update tree of object using asp.net webapi odata:
"updaterelatedobject method works when sourceproperty not collection."
my code provided below. got error when mehod "updaterelatedobject" called. can please advise wrong code , how update tree of objects (meaning object contains collection of child objects) using asp.net webapi odata v4.
var container = new container(new uri("http://johnalbert.com/myodatatest/odata")); product product = container.products.expand(p=> p.productitems).expand(p=>p.productinvoices).where(p => p.pid == guid.parse("28c508b8-f2dc-45c2-b401-7f94e79ab347")).firstordefault(); if (product != null) { product.name = product.name + "_modified"; var pitem1 = product.productitems[0]; product.productitems.remove(pitem1); container.updaterelatedobject(product, "productitems", pitem1); var pitem2 = product.productitems[0]; pitem2.price = 999; container.updaterelatedobject(product, "productitems", pitem1); var pinv1 = product.productinvoices[0]; product.productinvoices.remove(pinv1); container.updaterelatedobject(product, "productinvoices", pinv1); } container.updateobject(product); container.savechanges(savechangesoptions.batchwithsinglechangeset);
what want delete relationship between items in collection-valued navigation property of entity , itself. in such case deletelink() right method use. in case following code should work:
if (product != null) { var pitem1 = product.productitems[0]; var pitem2 = product.productitems[0]; var pinv1 = product.productinvoices[0]; container.deletelink(product, "productitems", pitem1); container.deletelink(product, "productitems", pitem2); container.deletelink(product, "productinvoices", pinv1); container.savechanges(); }
you may think above way isn't intuitive directly removing navigation items entity using .remove()
did. problem, entity tracking enabled using dataservicecollection<t>
can help. can use blog post tutorial how use dataservicecollection<t>
: http://blogs.msdn.com/b/odatateam/archive/2014/04/10/client-property-tracking-for-patch.aspx
Comments
Post a Comment