vendredi 3 juillet 2015

Preventing to Delete Master Record if Child Exist

I would like to ask, how to prevent deletion, if a foreign key exist in child record. I have a table Party and LPO. Party records exist in LPO. I want if Party_ID exist, it should not delete and gives an appropriate message that you can not delete record or LPO if this party is exist. End1 and End2 OnDelete both are set to None which already preventing but I wan to show a message to user. Should I do like this or any other way...

I have my DeleteConfirmed method like this.

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
    var myQuery = (from b in db.LPO where b.Party_Id == id select b.Party_Id).ToList();
    if (myQuery.Count <= 0)
    {
        Party party = db.Parties.Find(id);
        db.Parties.Remove(party);
        db.SaveChanges();
    }
    else
    { 
        //What to do here ... ? //return HttpNotFound();
    }
        return RedirectToAction("Index");
    }
}

Aucun commentaire:

Enregistrer un commentaire