Search code examples
odata

OData version4 deep insert


I am trying to find out how to deep insert in odata v4. Odata v4 specification says that deep insert is possible. But I have tried it in several ways but unable to find out the solution. Whenever I am trying to post an entity with its navigation data, it is being received null on server side. Anybody has any idea on this. Thanks


Solution

  • I don't know what service side do you use. However, Web API OData supports to post an entity with its navigation property.

    For example, you have a Post method in your controller:

    [HttpPost]
    public IHttpActionResult Post(Customer customer)
    {
      int key = _customers.Count();
      customer.Id = key + 1;
      _customers.Add(customer);
      return Created(customer);
    }
    

    You can issue a POST request on http://..../odata/Customers with the following sample request payload:

    User-Agent: Fiddler
    Host: localhost:33082
    Content-Type: application/json
    Content-Length: 134
    
    {
          "Id":9,
           "Name":"Customer #9",
           "Orders":[
            {
              "OrderId":2,"Price":3.3
            }
          ]
    }
    

    Thanks.