Reputation: 458
I use WebApi in MVC 4 with EF, Ninject. In client i use knockout.js end it's delete my entries from UI, but it's still in DB. Add, update action works fine.
Client code:
self.removeUser = function (user) {
var conj = ko.toJS(user);
var json = JSON.stringify(conj);
var Id = user.Id();
$.ajax({
url: API_URL + Id,
cache: false,
type: 'DELETE',
contentType: 'application/json; charset=utf-8',
data: '',
success: function () {
self.Users.remove(user);
}
});
}
WebAPI
// DELETE api/user/5
public HttpResponseMessage Delete(int id)
{
_userRepository.Delete(id);
return Request.CreateResponse(HttpStatusCode.NoContent);
}
Repository
public void Delete(int userId)
{
var user = Get(userId);
_db.Users.Remove(user);
}
Where to start? Which way to go?
Upvotes: 0
Views: 110
Reputation: 139758
If you want to delete an entity is is not enough to remove it form its collection. You need to call DbContext.SaveChanges()
in order to commit the transaction and persist your changes into the DB:
public void Delete(int userId)
{
var user = Get(userId);
_db.Users.Remove(user);
_db.SaveChanges();
}
Upvotes: 2