Search code examples
javascriptjquerywcfentity-frameworkwcf-data-services

Update a Table Per Hierarchy entity from JS or JQUERY


I have created a ENTITY FRAMEWORK model of a CARS table and made two TPH entities on EDMX designer and named them OLD CAR and NEW CAR, have set me CARS table to Abstract.

Now, I am accessing the CARS entity from JQUERY and I can do the following:

  • GET (working fine)
  • DELETE (working fine)

but I am not able to CREATE (POST) or UPDATE (PUT) into the derived inherited entities, it gives me the following error " Types information must be specified for types which are inherited"

I have exposed all of my entities from WCF Data Services

below is my code;

 function putData() {
          var url = "http://localhost:55153/WebSite3/WcfDataService1.svc/Cars(2)";

          var obj = '{"CarName": "Margalla", "CarModel" : "Honda"}';
          var r = window.JSON.stringify(obj);
          $.ajax({
              type: "PUT",
              url: url,
              data: obj,
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (msg) {
                  alert("Updated successful");
              },
              error: function (msg) {
                  alert(msg.responseText);
              }
          });
      }

Solution

  • The problem here is that the server doesn't know which type of car you're trying to insert (or modify).

    Try changing your payload to include the "odata.type" property. For example:

    var obj = '{
                "odata.type": "YourNamespace.OldCar", 
                "CarName": "Margalla", 
                "CarModel" : "Honda"
               }';
    

    The "odata.type" property is specific to the new JSON format (v3 OData only), so I would also suggest including the "DataServiceVersion" header on the request to make it clear to the server that you are sending a v3 payload. For example:

    $.ajax({
              ...
              beforeSend: function (request)
              {
                  request.setRequestHeader("DataServiceVersion", "3.0");
              },
              ...
           });