I was trying to implement the Delete Action in the MVC3, but I got to the point that the object that I want to delete has dependencies in other tables and that's why I am getting:
DELETE statement conflicted with the REFERENCE constraint
Here is my method in the controller:
[HttpPost]
public ActionResult Delete(int? id)
{
repo.DeleteDirector(id);
return View("index");
}
and this is how I am deleting it in my repository:
void DeleteDirector(int? id)
{
Director d = dc.Directors.FirstOrDefault(dir => dir.Id == id);
dc.Directors.Remove(d);
dc.SaveChanges();
}
I was hoping that simple LINQ Remove method will do the job, but it won't. So I am trying to think of a way to efficiently remove it with all the dependencies. I know that I can do it manually by going through all the tables where particular Director might be referenced. But I was hoping if Framework provides own implementation for it.