In the first post we have created a service which worked fine for retrieving data. most time we also need delete and create functionality. Here is how it works.
At the moment, the V3 Implementation of Microsoft does not support Create Functionality. You will run into a 415 Unsupported Media Type Exception. More information you can find on Stackoverflow.
The solution for this is quite simple. Just change the value for MaxProtocolVersion in your Service from "V3" to "V2" .
The difficult part of the Create/Delete Functionality was to create a custom IUpdatable Implementation. Well, this is NOT required for your implementation. You can just replace my implementation with your Entity-Framework Context.
I want my example independent from database/files. So i have created a static collection of items, controlled by the Context.
Replace CarContext.cs code with the following.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Services;
namespace MK.oDataConsuming.Web.Model {
publicclassCarContext : IUpdatable {
#region Members
privatestaticList<Car> _cars;
#endregion #region Constructor
public CarContext() {
if (_cars == null) {
_cars = newList<Car>();
}
}
#endregion #region Properties
publicIQueryable<Car> Cars {
get {
return _cars.AsQueryable<Car>();
}
}
#endregion #region IUpdatable Methods
publicvoid AddReferenceToCollection(object targetResource, string propertyName, object resourceToBeAdded) {
thrownewNotImplementedException();
}
publicvoid ClearChanges() {
thrownewNotImplementedException();
}
publicobject CreateResource(string containerName, string fullTypeName) {
Car car = newCar();
int tmpId = 0;
if (_cars.Count > 0) {
tmpId = _cars.Max(c => c.Id);
}
car.Id = ++tmpId;
return car;
}
publicvoid DeleteResource(object targetResource) {
List<Car> carsToDelete = (List<Car>)targetResource;
Car carToDelete = carsToDelete[0];
_cars.Remove(carToDelete);
}
publicobject GetResource(IQueryable query, string fullTypeName) {
List<Car> carList = query.Cast<Car>().ToList();
return carList;
}
publicobject GetValue(object targetResource, string propertyName) {
thrownewNotImplementedException();
}
publicvoid RemoveReferenceFromCollection(object targetResource, string propertyName, object resourceToBeRemoved) {
thrownewNotImplementedException();
}
publicobject ResetResource(object resource) {
thrownewNotImplementedException();
}
publicobject ResolveResource(object resource) {
if (resource isCar) {
_cars.Add((Car)resource);
}
return resource;
}
publicvoid SaveChanges() {
//We don't have any database/file to store the data in this test.
}
publicvoid SetReference(object targetResource, string propertyName, object propertyValue) {
thrownewNotImplementedException();
}
publicvoid SetValue(object targetResource, string propertyName, object propertyValue) {
if (propertyName == "Manufacturer") {
((Car)targetResource).Manufacturer = (string)propertyValue;
} elseif (propertyName == "ProductionYear") {
((Car)targetResource).ProductionYear = (string)propertyValue;
} else {
thrownewException("Property not implemented.");
}
}
#endregion
}
}
This is not a reference implementation of IUpdatable. At the moment i have not found a good documentation/example. So i have tested a little bit. It works for testing purposes.
Add new item (to Styles-Folder) --> Web --> Style Sheet
The javascript-part is extended with examples for create and delete. I also have added a UI Clear-Function. You can just comment out the first line of the "RetrieveData" Method if you want to see the history of your actions.
/*
* Retrieve data from service
**/
function RetrieveData() {
ClearView();
$.ajax({
type: "GET",
async: false,
contentType: "application/json; charset=utf-8",
datatype: "json",
url: "/CarService.svc/Cars",
success: RetrieveMultipleCallback,
error: function () {
alert("Error");
},
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json;odata=verbose");
}
});
}
/*
* Handles the RetrieveMultiple Response
**/
function RetrieveMultipleCallback(data) {
var $newDiv;
$.each(data.d, function (i, item) {
$newDiv = $("