public static void deleteMatch(Matching m, string donorEmail) { //db.Matching_tbl.Remove(Matching.DTOToc(m)); //this is becuse we nee dto mark the object in the context as deleted // ef will afterwards compare the context and find the changes and update as we have marked //we only need to use this way if we generate the object on our own //otherwise we can simply use remove() if retrieved from dbcontext using (BTProjectEntities db = new BTProjectEntities()) { bool oldValidateOnSaveEnabled = db.Configuration.ValidateOnSaveEnabled; try { db.Configuration.ValidateOnSaveEnabled = false; db.Entry(Matching.DTOToc(m)).State = EntityState.Deleted; //db.Matching_tbl.Attach(Matching.DTOToc(m)); db.SaveChanges(); } finally { db.Configuration.ValidateOnSaveEnabled = oldValidateOnSaveEnabled; } } using (BTProjectEntities db = new BTProjectEntities()) { //send emails to learner and donor List <Learner> l = Learner.cToDTO(db.learners_tbl.ToList()); Learner learner = l.FirstOrDefault(le => le.learnerId == m.learnerId); Email e = new Email(); //email donor e.sendEmailViaWebApi("", donorEmail, "ביטול השתתפות לומד בתוכנית לימוד", "C:\\Users\tzipp\\BTProject\\cheshvanProject\\BLL\\BLL\\cancelMatching.rtf", "", "", " הלומד" + learner.learnerName + "מזהה תוכנית הלימוד " + " " + m.reqId); //emailLearner e.sendEmailViaWebApi("", learner.learnerEmail, "ביטול השתתפות בתוכנית לימוד", "C:\\Users\tzipp\\BTProject\\cheshvanProject\\BLL\\BLL\\CancelMatchingLearner.rtf", "", "", "מזהה תוכנית הלימוד" + m.reqId + " מייל היזם:" + donorEmail); } //db.Entry(Matching.DTOToc(m)).State = EntityState.Deleted; //db.SaveChanges(); }