I created a simple MVC3 project just like the Music Store sample. Everything worked just fine. It was then time to port the database to a GoDaddy server and now I can’t delete items from the database.
This is the error:
The DELETE statement conflicted with the REFERENCE constraint \”Captions_Item_Captions\”. The conflict occurred in database \”mattymattmofo\”, table \”dbo.Captions\”, column ‘Item_ItemId’.\r\nThe statement has been terminated.
//
// POST: /NavigationManager/Delete/5
[Authorize(Roles = "Administrator")]
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Item item = db.Items.Find(id);
db.Items.Remove(item);
db.SaveChanges();
return RedirectToAction("Index");
}
Can anyone please help?
In your database Items (parent table) is linked with Captions (child table) by foreing key. That means when you are removing a row from Items table there still might be a connection to this row in Captions table. So constraint is generated to preserve database integrity. You could do two things to resolve this:
Right now your constraint delete rule is probably set to No Action and that’s why you are having this error.
You can set Delete Rule in Management Studio by right clicking on constraint (in Keys folder) -> Modify -> extend INSERT And UPDATE Spcecification -> Delete Rule.
Or by T-SQL script. There are many examples on how to do it.