public void DeleteSheetData(string RegistrationKey,List<long> SheetsForDeletion) { long DentalOfficeID=util.GetDentalOfficeID(RegistrationKey); try { if(DentalOfficeID==0) { return; } ODWebServiceEntities db=new ODWebServiceEntities(); for(int i=0;i<SheetsForDeletion.Count();i++) { long SheetID=SheetsForDeletion.ElementAt(i);// LINQ throws an error if this is directly put into the select expression // first delete all sheet field then delete the sheet so that a foreign key error is not thrown var delSheetField=from wsf in db.webforms_sheetfield where wsf.webforms_sheet.SheetID==SheetID select wsf; for(int j=0;j<delSheetField.Count();j++) { // the ElementAt operator only works with lists. Hence ToList() db.DeleteObject(delSheetField.ToList().ElementAt(j)); } var delSheet=from ws in db.webforms_sheet where ws.SheetID==SheetID select ws; db.DeleteObject(delSheet.First()); Logger.Information("deleted SheetID="+SheetID+" DentalOfficeID="+DentalOfficeID); } db.SaveChanges(); Logger.Information("In DeleteSheetData IpAddress="+HttpContext.Current.Request.UserHostAddress+" DentalOfficeID="+DentalOfficeID); } catch(Exception ex) { Logger.LogError("IpAddress="+HttpContext.Current.Request.UserHostAddress+" DentalOfficeID="+DentalOfficeID,ex); } }
public void DeleteSheetDef(string RegistrationKey,long WebSheetDefID) { long DentalOfficeID=util.GetDentalOfficeID(RegistrationKey); try { if(DentalOfficeID==0) { return; } ODWebServiceEntities db=new ODWebServiceEntities(); webforms_sheetdef SheetDefObj=null; var SheetDefResult=db.webforms_sheetdef.Where(sd=>sd.WebSheetDefID==WebSheetDefID); if(SheetDefResult.Count()>0) { SheetDefObj=SheetDefResult.First(); //load and delete existing child objects i.e sheetfielddefs objects SheetDefObj.webforms_sheetfielddef.Load(); var SheetFieldDefResult=SheetDefObj.webforms_sheetfielddef; while(SheetFieldDefResult.Count()>0) { db.DeleteObject(SheetFieldDefResult.First());//Delete SheetFieldDefObj } db.DeleteObject(SheetDefResult.First());//Delete SheetDefObj Logger.Information("deleted WebSheetDefID="+WebSheetDefID+" DentalOfficeID="+DentalOfficeID); } db.SaveChanges(); Logger.Information("In DeleteSheetDef IpAddress="+HttpContext.Current.Request.UserHostAddress+" DentalOfficeID="+DentalOfficeID); } catch(Exception ex) { Logger.LogError("IpAddress="+HttpContext.Current.Request.UserHostAddress+" DentalOfficeID="+DentalOfficeID,ex); } }