1
votes

First of all I am new to .NET WCF oData DataService and I am using jQuery on the client side. I am looking for a better way (right way) to handle adding 2 different entities in one transaction (post).

Eg. I have a Parent entity with ID and Name and Child entity with ID,ParentID and Name.

I would like to add parent and get parent's id and use it to add each child. However, while adding either the parent or one of the child entity fails, then it should roll back all the transactions. One way, I thought of doing it is by creating a WebInvoke Method which would take a json string like this {"Parent":{"name":"Daddy"},"Children":[{"name":"Tom"},{"name":"Dick"},{"name":"Harry"}]} While testing I hardcoded argument in the method, on invoke everything worked fine. However, when calling POST from jQuery, the parameter comes in as NULL (annoying). I was able to send parameter to the method by passing the urlencoded json string as part of query string. However, query string has size limit and I prefere not to use it.

Question 1) Is it possible to use WebInvoke POST with parameter(s) in WCF Data Service? Question 2) Is there a better way (right way) to do what I am trying to do.

Another way of doing it (I don't like it) is to call POST on parent entity and get its ID and using it to post for each child. If one of it fails then call delete for all.

1

1 Answers

1
votes

Currently the service operation (the WebInvoke method) doesn't support passing parameters in the request body, only in the URL. The right solution for this is to use batching. The client can send all the operations in a single batch request (with a single changeset in it) and then the server will try to apply them and if one of them fails the entire changeset is rolled back. Creating a batch payload is a bit complicated but there's a java script library datajs which will help you with that. In fact it will help you with the OData protocol all up. http://datajs.codeplex.com/